Consulting

Results 1 to 5 of 5

Thread: VBA Code to every other instance of a string of characters

  1. #1

    VBA Code to every other instance of a string of characters

    Hi all,

    Don't have any experience with VBA, but I need to create a quick macro that converts every other instance of string of characters in Word document. Below is what I have, which looks for every other double underscore "__" and adds a star to the end of one of them. Unfortunately, it only seems to work once per line, so it isn't counting them correctly. It also only works unreliably if the underscores are next to another character, such as around a word like __down__.

    Sub Macro1()
    i = 0
    k = "__"
    For Each k In ActiveDocument.Words
        If (k = "__") Then
            i = i + 1
            k.Select
    
    
            If (i Mod 2) = 0 Then
                blanks = ""
                For Counter = 1 To Len(k) - 1
                    blanks = blanks + "__*"
                Next Counter
                Word.Selection.TypeText (blanks)
            End If
        End If
    Next k
    
    
    
    
    End Sub
    Using that code on this line:
    _
    $__
    __Down__ __Hill__
    __
    ^__
    __ __ __
    __
    Produces this:

    _
    $__
    __Down__ __*Hill__
    __*
    ^__
    __ __ __
    __*
    :
    It should look like this:
    _
    $__
    __*Down__ __*Hill__
    __*
    ^__
    __* __ __*
    __
    If someone could assist me with this that'd be awesome.

    Thanks for any and all help!

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Have you step debugged and examined content of variables as code executes? Use Debug.Print to examine variables at critical steps in code. Use Watch window.

    Try this following the For Each line:

    Debug.Print i & " : " & k

    Examine output in the Immediate Window.

    After couple hours of testing, this produces desired output from given sample:
    Sub Macro1()
    Dim i, k, blanks, counter
    i = 1
    For Each k In ActiveDocument.Words
        If InStr(k, "__") > 0 And InStr(k, "*") = 0 Then
            k.Select
            If (i Mod 2) = 0 Then
                blanks = ""
                For counter = 1 To Len(Trim(k)) - 1
                    blanks = blanks & "__*" & IIf(InStr(k, " ") > 0, " ", "")
                Next counter
                Word.Selection.TypeText blanks
            End If
            If InStr(k, "*") = 0 Then i = i + 1
        End If
    Next k
    End Sub
    Last edited by June7; 12-14-2023 at 08:58 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Quote Originally Posted by June7 View Post
    Have you step debugged and examined content of variables as code executes? Use Debug.Print to examine variables at critical steps in code. Use Watch window.

    Try this following the For Each line:

    Debug.Print i & " : " & k

    Examine output in the Immediate Window.

    After couple hours of testing, this produces desired output from given sample:
    Sub Macro1()Dim i, k, blanks, counter
    i = 1
    For Each k In ActiveDocument.Words
        If InStr(k, "__") > 0 And InStr(k, "*") = 0 Then
            k.Select
            If (i Mod 2) = 0 Then
                blanks = ""
                For counter = 1 To Len(Trim(k)) - 1
                    blanks = blanks & "__*" & IIf(InStr(k, " ") > 0, " ", "")
                Next counter
                Word.Selection.TypeText blanks
            End If
            If InStr(k, "*") = 0 Then i = i + 1
        End If
    Next k
    End Sub
    Dang a couple of hours?! I appreciate you!

    I spent like 4 hours on this, I thought I was very close. I did not try to debug, but I'm going to try that, not sure how to step debug but I'll Google to see if I can replicate what you created because honestly I don't understand it.

    Thanks again!

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Have a look here as Mynda explains debugging. https://www.myonlinetraininghub.com/debugging-vba-code
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Dang, forum merged first two lines of code. No idea why that happens. Put the Dim statement on its own line. I will edit previous post.

    Code modules should have Option Explicit at top which forces variable declaration. That's why I added the Dim statement. But I was lazy and did not declare data types so they are all Variant by default.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Posting Permissions

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