Consulting

Results 1 to 3 of 3

Thread: Add Number of Times - Loop Array

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Add Number of Times - Loop Array

    Add Number of Times - Loop Array

    folks good day,

    and nice to see all.

    I had a idea to space out some text so i wanted to say insert new lines to space out the blocks so i can see them better


      Sub Space_Out_Text()
    
    
        
        Dim oRng As Word.Range
        Dim oTargetParagraphs As Variant
        Dim xAfterText As Variant
        Dim i As Long, j As Long
        
         
        Dim oPara As Word.Paragraph
        
    
        
        oTargetParagraphs = Array("#MX18", "#JK78")   '
    
    
        xBeforeText = Array(vbNewLine & vbNewLine, vbNewLine & vbNewLine)                           ' Insert Before
    
        xAfterText = Array(vbNewLine & vbNewLine&vbNewline, vbNewLine & vbNewLine)             ' Insert After
    
    
    
            For i = 0 To UBound(oTargetParagraphs)
                Set oRng = ActiveDocument.Range
                j = 0
                With oRng.Find
                
                Do While .Execute(FindText:=oTargetParagraphs(i), MatchWholeWord:=True)
                    
                If .Found Then
                oRng.Expand Unit:=wdParagraph
                
                
                oRng.Words.First.InsertBefore xBeforeText(i)
            
                oRng.Words.Last.InsertBefore xAfterText(i)
                
                
                End If
                oRng.Collapse 0
                Loop
                End With
            Next i
            Set oRng = Nothing
    
       
    End Sub

    now if for example i wanted 10 vb lines for an array term, i would hate to have to do this
    vbNewLine & vbNewLine &vbNewLine & vbNewLine& vbNewLine & vbNewLine in the array for one target paragraph

    that looks very unelegant


    is there a better way for me to insert a number of vblines without having to repeat them in the array?

    Is there a more efficient way for me to say I want 10 vblines inserted , another term in the array i may want 5 vblines

    well its very confusing to explain but i hope i made sense

    and thank you
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Perhaps something like:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim ArrFnd As Variant, ArrP As Variant, ArrS As Variant
    Dim i As Long, j As Long, StrP As String, StrS As String
    ArrFnd = Array("#MX18", "#JK78", "#AO45", "#BC39")
    ArrP = Array(1, 1, 3, 4)
    ArrS = Array(1, 1, 2, 2)
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .MatchWholeWord = True
      .MatchCase = True
      .Format = False
      .Forward = True
      .Wrap = wdFindContinue
      For i = 0 To UBound(ArrFnd)
        StrP = "": StrS = ""
        For j = 1 To ArrP(i)
          StrP = StrP & Chr(11)
        Next
        For j = 1 To ArrS(i)
          StrS = StrS & Chr(11)
        Next
        .Text = ArrFnd(i)
        .Replacement.Text = StrP & "^&" & StrS
        .Execute Replace:=wdReplaceAll
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Paul,


    Thanks for that really made my day because I couldn't do it

    I tried all sorts of arrays and multidimensional things again and well in the end I just had to copy and paste my VB line 20 times

    And I hate having to manually move the text because there was so much of it and
    I couldn't find what I was looking for and well I got a bit tired of pasting in the wrong place

    Now I can add all the numbers of lines that I need to space out because some bits of text I don't need that much space

    But there are other blocks of text that are so condensed that I couldn't see
    so those ones need a lot of space so I can now say i want 10 lines without having to make too much of a mess of the text

    And when I'm done I can just delete all the empty lines and hey presto it's back to how it was without anyone knowing that I messed too much with the text well i am a bit clumsy

    Well that was the fastest writing code and a new skill that I learnt - to make 3 arrays now

    its my new shiny toy thanks so much

    Paul and hope you have a great day!!

    and forum too
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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