在特定的工作表中search一个文本string

我试图让excelsearch特定工作表中不是活动工作表的特定列中的文本string。 VBA给我一个错误,说我不能使用这种select方法。 所以我的问题是,你有build议以另一种方式做?

Worksheets("Parts for renovation").Columns("Q:Q").Select Set cell = Selection.Find(What:="Total transfer price", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If cell Is Nothing Then Exit Sub Else Worksheets("Internal").Cells(29, 4) = Worksheets("Parts for Renovation").ActiveCell.Offset(0, 4) End If 

没有必要在那里select任何东西:

 With Worksheets("Parts for renovation").Columns("Q:Q") Set cell = .Find(What:="Total transfer price", After:=.Cells(1), LookIn:=xlFormulas, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If cell Is Nothing Then Exit Sub Else Worksheets("Internal").Cells(29, 4) = Cell.Offset(0, 4) End If End With 

您的错误出现了,因为您在非活动工作表上select一个范围。 这就是为什么你应该避免select一般的原因之一。

但是,如果要使代码正常工作(强烈build议不要这样做),则可以考虑在select范围之前select工作表:

 Worksheets("Parts for renovation").Select Columns("Q:Q").Select 

对于最好的部分,尽量避免使用“select” – 如何避免使用Excel中的selectVBA