PDA

View Full Version : VBA Code to every other instance of a string of characters



CodeConvert
12-14-2023, 03:20 PM
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!

June7
12-14-2023, 05:34 PM
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

CodeConvert
12-14-2023, 08:09 PM
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!

Aussiebear
12-14-2023, 08:45 PM
Have a look here as Mynda explains debugging. https://www.myonlinetraininghub.com/debugging-vba-code

June7
12-14-2023, 08:58 PM
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.