Consulting

Results 1 to 5 of 5

Thread: Align a line based on a value in the line

  1. #1
    VBAX Newbie
    Joined
    Oct 2019
    Posts
    1
    Location

    Question Align a line based on a value in the line

    I have a text file with over 50,000 rows (lines).
    There's a character in each line - say 'XX' and 'YY', which doesn't occur otherwise


    For each line with 'XX', I want the line to be left aligned, and for 'YY' I need it right aligned. (As of now, all the lines are left aligned.)
    There's no particular sequence in XX or YY type repetition. (XX and YY type lines occur randomly.) And at each change from XX to YY or vice versa, I need a new line character.


    Let me clarify this with an example.

    Data:

        (Left-aligned) XX: ABCD
        (Left-aligned) YY: EFGH
        (Left-aligned) YY: IJK
        (Left-aligned) XX: LMN
        (Left-aligned) XX: OPQ
        (Left-aligned) YY: STU

    Output Required:


        (Left-aligned) XX: ABCD
    
    
        (Right-aligned) YY: EFGH
        (Right-aligned) YY: IJK
    
    
        (Left-aligned) XX: LMN
        (Left-aligned) XX: OPQ
    
    
        (Right-aligned) YY: STU
    I tried with this but it doesn't give desired result. Please guide me to fix the code to get the result.

    Private Sub alignText()
        With Selection.Find.Execute FindText:="YY", Forward:=True
             Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
        End With
    End Sub

  2. #2
    How about the following, however note that text files do not support such formatting.

    Private Sub alignText()
    Dim orng As Range
    Set orng = ActiveDocument.Range
        With orng.Find
            Do While .Execute(findText:="YY", Forward:=True)
                orng.Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
                orng.Collapse 0
            Loop
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Alignment in Word is a paragraph (not line) attribute. So unless each line is a paragraph, there isn't much you can do. If each line is a paragraph, and as the text with "XX" is already left aligned then all you need is:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oPar As Paragraph
      For Each oPar In ActiveDocument.Paragraphs
        If InStr(oPar.Range.Text, "YY") Then oPar.Alignment = wdAlignParagraphRight
      Next oPar
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Opps, Graham beat me to the reply. In a large large document like you describe, his method would be faster.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Howsoever, this would be faster yet:

    Private Sub alignText()
    Dim orng As Range
    Set orng = ActiveDocument.Range
      With orng.Find
        .Text = "YY"
        .Replacement.ParagraphFormat.Alignment = wdAlignParagraphRight
        .Execute Replace:=wdReplaceAll
      End With
    lbl_Exit:
     Exit Sub
    End Sub
    Thanks Graham, for the stoking the brain to think!!
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •