VBA查找函数抛出错误

时间:2018-05-29 10:33:44

标签: excel vba excel-vba

我无法让此代码停止抛出错误

  

'需要对象'

我必须将.Activate放在所有内容之前,现在它转到set cf。找到然后抛出一个错误。我试图根据输入框搜索信息,然后根据搜索条件将用户表单中的信息放入单元格。请有人帮忙。感谢

Worksheets(" MthruF Schedule").Activate     
Dim Req As Variant
Req = InputBox("Please Enter Requisition Number", "Information")    

'Worksheets(" MthruF Schedule").Range("A1").Activate   

Dim FirstAddress As String, cF As Range

With ThisWorkbook.Sheets(" MthruF Schedule").Range("A1").Cells(3, 1).Activate 

    'First, define properly the Find method
    Set cF = .Find(What:=Req, _
              after:=ActiveCell, _
              LookIn:=xlFormulas, _
              LookAt:=xlPart, _
              SearchOrder:=xlByColumns, _
              SearchDirection:=xlNext, _
              MatchCase:=False, _
                    SearchFormat:=False)                    

    'If there is a result, keep looking with FindNext method
    If Not cF Is Nothing Then
        FirstAddress = cF.Address
        Do
            cF.Offset(0, 9).Value = EmpInfo.FirstName.Value & " " & EmpInfo.LastName.Value
            cF.Offset(0, 10).Value = EmpInfo.ComboBox3.Value & "/" & EmpInfo.ComboBox20.Value & "/" & EmpInfo.YearCmb.Value
            cF.Offset(0, 11).Value = EmpInfo.AgencyBox.Value
            cF.Offset(0, 12).Value = EmpInfo.AgencyName.Value
            Set cF = .FindNext(cF)
        'Look until you find again the first result
        Loop While Not cF Is Nothing And cF.Address <> FirstAddress
    End If

End With

2 个答案:

答案 0 :(得分:1)

我需要激活工作表,而不是工作表中的单元格。我在上面的代码之前添加了以下内容,以完成这个漫长而恼人的过程:

    Dim ws As Worksheet


 Set ws = Sheets(" MthruF Schedule")

 ws.Activate

答案 1 :(得分:0)

您的<img src='http://127.0.0.1:9999/api/v1/testbarcode' /> 阻止声明为with.Activate没有其他方法,因此它不知道如何处理.Activate。如果您将.Find放入with block中,您将获得所需的结果

.Activate