This is a continuation of the question Do Until IsEmpty to loop through the user-defined ranges:
In the code below the excel Macro loops through Original text range, replacing all the instances of "tagname" with the assigned loopText in cell D2 and puts the corrected text into cell B9. Then it checks whether the looptext range has other values and loops through the original text again to account for these. Once the looptext cell is empty, the macro stops and prints the corrected text for each instance of loopText. Parallel to replacing "tagname" in cells D2 and onward I want the Macro to replace the word "sheetname" with the range of cells in E2 and onward. I have created a separate Do Until IsEmpty loop, however excel only executes the first one.
Please, help locate an error in my VB code.
Thank you.
Sub CommandButton21_Click()
Dim correctedText2 As Range
Dim OriginalText2 As Range
Dim loopText1 As Range
Dim loopText2 As Range
Dim cel As Range
Dim i As Long
Dim j As Long
Dim k As Long
Set OriginalText2 = Range("H3:H20")
Set correctedText2 = Range("B9")
Set loopText1 = Range("D2")
Set loopText2 = Range("E2")
i = 0
j = 0
Do Until IsEmpty(loopText1.Offset(j).Value)
For Each cel In OriginalText2
correctedText2.Offset(i).Value = Replace(cel.Value, "tagname", loopText1.Offset(j).Value)
i = i + 1
Next cel
j = j + 1
Loop
k = 0
Do Until IsEmpty(loopText2.Offset(k).Value)
For Each cel In OriginalText2
correctedText2.Offset(i).Value = Replace(cel.Value, "sheetname", loopText2.Offset(k).Value)
i = i + 1
Next cel
k = k + 1
Loop
End Sub
correctedText2in both loops? - findwindow