I have 3 columns that have the same pattern. Here is the example:
"
I Love Chocolate
I really love chocolate
I want to drink hot chocolate
I have a red bike
I buy it with my own money
I hate mouse
I hate mouse since I was little
"
I want to add semicolon before each of the line of bold texts in the cell. Like this:
"
;I Love Chocolate
I really love chocolate
I want to drink hot chocolate
;I have a red bike
I buy it with my own money
;I hate mouse
I hate mouse since I was little
"
I used a Macro like this, but it doesn't work. It gave no error warning; it just doesn't work as I wanted it to be.
Sub AddSemicolonBeforeBoldText()
Dim rng As Range
Dim cell As Range
Dim text As String
Dim startPos As Integer
Dim endPos As Integer
Set rng = ActiveSheet.UsedRange
For Each cell In rng
If cell.HasFormula Then
' Skip cells with formulas
GoTo ContinueLoop
End If
text = cell.Value
startPos = 1
Do While startPos <= Len(text)
startPos = InStr(startPos, text, "*", vbTextCompare)
If startPos = 0 Then Exit Do
endPos = InStr(startPos + 1, text, "*", vbTextCompare)
If endPos = 0 Then Exit Do
' Insert a semicolon before the bold text
text = Left(text, startPos - 1) & ";" & Mid(text, startPos)
startPos = endPos + 1 ' Move the start position after the second asterisk
Loop
cell.Value = text
ContinueLoop:
Next cell
End Sub
What did I do wrong?