我正在Excel电子表格中编写一个宏,以便用另一个单元格的内容替换一个单元格中的值,并在每次看到该单词时循环显示替换相同值的原始文本。 例如,我在单元格范围内有一个文本,其中每行都有一个单词“tagname”,我想用同一电子表格的单元格A1的值替换“tagname”,例如说“Maggie”,而不是标签名。 这是我的代码至今:需要用另一个单元格替换单元格中的值
Private Sub CommandButton21_Click()
Dim OriginalText As Range
Dim CorrectedText As Range
'definition of ranges
Set OriginalText = Range("H4:H10")
'setting of ranges
For Each OriginalText In CorrectedText
CorrectedText.Value = Replace(OriginalText.Value, "tagname", Range("D2").Value)
Next OriginalText
'a loop through the original text to replace the word "tagname" with the value of cell D4
Columns(2).Clear 'clear column 2 for the Corrected Text
Range("A24:A30").Offset(, 1).Value = CorrectedText
'copy corrected text in these cells
End Sub
我得到运行时错误424,对象。
最后一行应该是'范围。( “A24:A30”)偏移(1).value的= CorrectedText.Value' –
那被说你不需要循环,看来你正在替换。而不是整个循环只是放置这个:'correctedText.Replace什么:=“标记名”,替换:=范围(“D2”)' –