我知道这是完全错误的,但此刻,我真的不知道该怎么做。
在Microsoft Excel中,我想替换" OldValues"上的所有值。字符串,由固定字符串" NewValue"。怎么可能?
LBound和Ubound使用不对,对吧?
Sub Replace()
Dim sht As Worksheet
Dim OldValues As Variant
Dim NewValue As String
Dim x As Long
OldValue = Array("old1","old2","old3")
NewValue = "allnew!"
For x = LBound(OldValue) To UBound(NewValue)
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=OldValue(x), Replacement:=NewValue(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
End Sub
答案 0 :(得分:4)
您的代码应该进行微小的更改:
NewValue
不是数组,因此UBound(NewValue)
会出错。您必须在循环中转到UBound(OldValues)
,然后移除(x)
中NewValue
之后的Replace
。
Sub Replace()
Dim sht As Worksheet
Dim OldValues As Variant
Dim NewValue As String
Dim x As Long
OldValues = Array("old1","old2","old3")
NewValue = "allnew!"
For x = LBound(OldValues) To UBound(OldValues)
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=OldValues(x), Replacement:=NewValue, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Next x
End Sub