VBA搜索表填写用户表单

时间:2018-06-07 13:58:56

标签: excel vba excel-vba

我正在尝试使用查找功能,它将在工作表上找到答案并填写我的用户表单。这将消除必须真正看到工作表的需要。它已成为更多自己的应用程序。

我收到错误:

    JobTitle.value = cF.Offset(0,1).value

有更好的方法吗?我尝试过为双方创建一个变量,但似乎都没有效果。错误是:

  

对象变量或未设置块变量

Private Sub CommandButton4_Click()

    JobTitle.Visible = True
    Label2.Visible = True
    Label3.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    Label6.Visible = True
    TextBox1.Visible = True
    TextBox2.Visible = True
    TextBox3.Visible = True
    TextBox4.Visible = True

    Dim ws As Worksheet
    Set ws = Sheets(" MthruF Schedule")

    ws.Activate
    Dim PartNumber As Variant
    PartNumber = SearchBar.Value

    'InputBox("Please Enter Requisition Number", "Information")

    Dim FirstAddress As String, cF As Range

    With ThisWorkbook.Sheets(" MthruF Schedule").Range("A:A")
        .Cells(1, 1).Activate
        'First, define properly the Find method
        Set cF = .Find(What:=PartNumber, _
                       after:=ActiveCell, _
                       LookIn:=xlValues, _
                       LookAt:=xlWhole, _
                       SearchOrder:=xlByColumns, _
                       SearchDirection:=xlNext, _
                       MatchCase:=True, _
                       SearchFormat:=False)

        JobTitle.Value = cF.Offset(0, 1).Value
        TextBox1.Value = cF.Offset(0, 8).Value
        TextBox2 = cF.Offset(0, 9).Value
        TextBox3 = cF.Offset(0, 10).Value
        TextBox4 = cF.Offset(0, 13).Value

        'If there is a result, keep looking with FindNext method
        If Not cF Is Nothing Then
            FirstAddress = cF.Address
            Do
                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

End Sub

0 个答案:

没有答案