我正在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」)' –