我不知道下面的代码有什么问题,我正在寻找解决方案或任何提示。
目标是在2个单独的工作表中命名8个单独的范围,并创建一个Find&替换功能。
工作表也被命名,但我在引用前4个Excel范围(topAR,topExp,subAR和subExp)时收到错误,但无法进入Find&替换功能。
我欢迎您提供任何帮助。提前谢谢!
Sub FindReplace()
Dim topAR, topExp, subAR, subExp As Range
Dim topFind As Variant, topReplace As Variant
Dim subFind As Variant, subReplace As Variant
Dim wsTop As Worksheet, wsSub As Worksheet
wsTop = Workbook.Sheets("Top-20")
wsSub = Workbook.Sheets("Top US Sub-IG")
Set topAR = wsTop.Range.Cells("C8:E28")
Set topExp = wsTop.Range.Cells("M8:O28")
Set subAR = wsSub.Range.Cells("D7:F22")
Set subExp = wsSub.Range.Cells("L7:N22")
topFind = wsTop.cell.Location("H2")
topReplace = wsTop.cell.Location("I2")
subFind = wsSub.cell.Location("H2")
subReplace = wsSub.cell.Location("I2")
Range.topAR.Replace What:="topFind", Replacement:="topReplace", LookAt:= _
xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Range.topAR.Replace What:="subFind", Replacement:="subReplace", LookAt:= _
xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False
End Sub
答案 0 :(得分:0)
您必须采用适当的语法来引用Range对象
可能这就是你所追求的
Sub FindReplace()
Dim topAR As Range, topExp As Range, subAR As Range, subExp As Range
Dim topFind As Variant, topReplace As Variant
Dim subFind As Variant, subReplace As Variant
Dim wsTop As Worksheet, wsSub As Worksheet
Set wsTop = ActiveWorkbook.Sheets("Top-20")
Set wsSub = ActiveWorkbook.Sheets("Top US Sub-IG")
Set topAR = wsTop.Range("C8:E28")
Set topExp = wsTop.Range("M8:O28")
Set subAR = wsSub.Range("D7:F22")
Set subExp = wsSub.Range("L7:N22")
Set topFind = wsTop.Range("H2")
Set topReplace = wsTop.Range("I2")
Set subFind = wsSub.Range("H2")
Set subReplace = wsSub.Range("I2")
topAR.Replace What:=topFind.Value, Replacement:=topReplace.Value, LookAt:= _
xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
subAR.Replace What:=subFind.Value, Replacement:=subReplace.Value, LookAt:= _
xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub