vba查找在电子表格中查找信息

时间:2017-10-03 22:49:48

标签: vba lookup

enter image description here您好我试图为自己查找但是遇到了一些麻烦,

 Private Sub CommandButton3_Click()
 Dim cell As Range
 Dim iRow As Long
 Dim ws As Worksheet
 Set ws = Worksheets("Sheet1")

 If WorksheetFunction.CountIf(Sheet1.Range("A:A"), Me.textbox_GRN1.Value) = 0 
 Then
 MsgBox "This is and incorrect GRN"
 Me.textbox_GRN1.Value = ""
 Exit Sub
 End If
 Dim rng As Range

 Set rng = ws.Range("A2")

 With ws
    Me.textbox_BAY1.Value = Application.WorksheetFunction.VLookup(rng, 
ws.Range("B1:B65536").Value, 1, False)
    Me.textbox_ROW1.Value = Application.WorksheetFunction.VLookup(rng, ws.Range("C1:C65536").Value, 1, False)
    Me.textbox_COLOUM1.Value = Application.WorksheetFunction.VLookup(rng, ws.Range("D1:D65536").Value, 1, False)
    Me.textbox_PALLET1.Value = Application.WorksheetFunction.VLookup(rng, ws.Range("E1:E65536").Value, 1, False)
 End With
 End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButton3_Click()

    Dim f As Range, GRN

    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")

    GRN = Me.textbox_GRN1.Value

    Set f = ws.Columns(1).Find(GRN, lookat:=xlWhole)

    If f Is Nothing Then
        MsgBox "This is an incorrect GRN"
        'Me.textbox_GRN1.Value = "" 'Don't do this!
                                    'It will annoy your users
        'you should probably clear the other textboxes here though
    Else
       With f.EntireRow
           Me.textbox_BAY1.Value = .Cells(2).Value
           Me.textbox_ROW1.Value = .Cells(3).Value
           Me.textbox_COLOUM1.Value = .Cells(4).Value
           Me.textbox_PALLET1.Value = .Cells(5).Value
       End With
    End If

End Sub

我更喜欢这样的东西:

_temporary