这是讨论Looping through values in cells to replace in the text
的延续以下代码获取单元格D2中的值,并在单词"标记名"的所有实例中在OriginalText中填充其值。用来。 然后循环遍历loopText范围并生成modifiedText范围。 我希望使用Do UntilEmpty命令循环,以便loopText范围取决于用户范围输入,例如,如果用户填充单元格D2:D54,那么所有这些都将被执行。
以下是我的代码,但我不断收到运行时错误' 91'
对象变量或未设置块变量
请帮忙。 感谢。
Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim loopText As Range
Dim i As Long
Dim cel As Range
Dim cel2 As Range
Do Until IsEmpty(loopText.Value)
Set correctedText = Range("B10")
Set OriginalText = Range("H3:H20")
Set loopText = Range("D2")
i = 0
Columns(2).Clear
For Each cel2 In loopText
For Each cel In OriginalText
correctedText.Offset(i).Value = Replace(cel.Value, "tagname", cel2.Value)
i = i + 1
Next cel
Next cel2
Loop
End Sub
答案 0 :(得分:0)
试试这个:
你只需要两个循环,第一个循环被改变为你正在寻找的东西。
Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim loopText As Range
Dim i As Long
Dim cel As Range
Dim cel2 As Range
Dim j As Long
Set correctedText = Range("B10")
Set OriginalText = Range("H3:H20")
Set loopText = Range("D2")
i = 0
j = 0
Columns(2).Clear
Do Until IsEmpty(loopText.Offset(j).Value)
For Each cel In OriginalText
correctedText.Offset(i).Value = Replace(cel.Value, "tagname", loopText.Offset(j).Value)
i = i + 1
Next cel
j = j + 1
Loop
End Sub