使用Cell.Find和If语句时出现'Oject required'错误

我正在研究一些VBA,它将在工作表中search特定的单词或短语,如果遇到这个单词或短语,我想要它抓住整个列并复制它。 不幸的是,当我尝试将search结果设置为等于我的variables时,我一直在遇到问题。

Dim Srch As Range Set Srch = Cell.Find(What:="Foundation Account", After:=ActiveCell, LookIn:= _ xlValues, SearchOrder:=xlByRows, SearchDirection:= _ xlNext, MatchCase:=False) If Not Srch Is Nothing Then 'Do something 

每次我运行这个,我得到一个运行时错误424'所需的对象'。 有没有原因,我无法设置Cell.Find = Cell.Find

Cell.findexpression式的语法错误 – 您必须定义它所查找的范围。 尝试将其更改为ActiveSheet.Cells.Find(.......)

  Dim ws as worksheet 'delcaring the rest of the variables ws.Cells.find(......) 

在将来 – 尝试阅读你正在尝试使用的方法和function的msdn – 这就是我今天所学到的几乎所有我知道的。 所以,当然:)

你可以尝试像这样:

 Public function findWord(sheetName as string, whatimlookingfor as string) Dim Srch As Range Dim oWs as Worksheet Dim oWb as Workbook Set oWb = ThisWorkbook Set oWs = oWb.Sheets(sheetName) Set Srch = oWs.Cell.Find(What:=whatimlookingfor, After:=oWs.Range("A1"), LookIn:= _ xlValues, SearchOrder:=xlByRows, SearchDirection:= _ xlNext, MatchCase:=False) If Not Srch Is Nothing Then oWs.Columns(Srch.column).Select Selection.Copy End if End function