'所需的对象'将Cell.Find与If语句

时间:2015-09-22 18:07:22

标签: excel vba excel-vba

我正在研究某个VBA,它会在工作表中搜索特定的单词或短语,如果遇到该单词或短语,我希望它能抓住整个列并复制它。不幸的是,当我尝试将搜索结果设置为等于我的变量时,我一直遇到问题。

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'需要对象'。我有理由无法设置Srch = Cell.Find吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

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