我有一个代码,我在一个不同的宏和wotked中使用,现在不能正常工作,我在激活的行中不断得到运行时错误1004:
Sub initialize()
dim MonthName as string
dim MainWB as workbook
dim VisualWS as worksheet
MonthName = cells (1,1).value
Set MainWB = ThisWorkbook
Set VisualWS = MainWB.Worksheets("Visual")
With VisualWS
.Range("L1:W1").Find(MonthName, , xlValues, xlWhole).Activate
End With
MonthCol = ActiveCell.Column
End Sub
答案 0 :(得分:1)
根据docs on Range.Find,如果找不到匹配项,"方法会返回Nothing
。"
最有可能的是,你的.Range()。Find()方法找不到任何匹配项,因此返回Nothing
,它没有函数Activate
。
请改为尝试:
Dim findResult As Range
Set findResult = .Range("L1:W1").Find(MonthName, , xlValues, xlWhole)
If findResult <> Nothing then findResult.Activate
答案 1 :(得分:0)
Find
可能会返回Nothing
,所以我宁愿写一下:
With VisualWS
set target = .Range("L1:W1").Find(MonthName, , xlValues, xlWhole)
End With
If target is Nothing Then
DoSomething
else
MonthCol = target.Cells(1,1).Column
End if
(未经测试)