Results 1 to 5 of 5

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    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.

Posting Permissions

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