vba lookup to lookup info in a spread sheet
vba lookup to lookup info in a spread sheet
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
Private Sub CommandButton3_Click()
Dim cell As Range, GRN
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
GRN = Me.textbox_GRN1.Value
If WorksheetFunction.CountIf(Sheet1.Range("A:A"), GRN) = 0 Then
MsgBox "This is an incorrect GRN"
'Me.textbox_GRN1.Value = "" 'Don't do this!
'It will annoy your users
Exit Sub
End If
Dim rng As Range
Set rng = ws.Range("A1").CurrentRegion 'assumes no blank rows/columns in data table
Me.textbox_BAY1.Value = Application.VLookup(GRN, rng, 2, False)
Me.textbox_ROW1.Value = Application.VLookup(GRN, rng, 3, False)
Me.textbox_COLOUM1.Value = Application.VLookup(GRN, rng, 4, False)
Me.textbox_PALLET1.Value = Application.VLookup(GRN, rng, 5, False)
End Sub
不过我更喜欢这样的东西:
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
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
Private Sub CommandButton3_Click()
Dim cell As Range, GRN
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
GRN = Me.textbox_GRN1.Value
If WorksheetFunction.CountIf(Sheet1.Range("A:A"), GRN) = 0 Then
MsgBox "This is an incorrect GRN"
'Me.textbox_GRN1.Value = "" 'Don't do this!
'It will annoy your users
Exit Sub
End If
Dim rng As Range
Set rng = ws.Range("A1").CurrentRegion 'assumes no blank rows/columns in data table
Me.textbox_BAY1.Value = Application.VLookup(GRN, rng, 2, False)
Me.textbox_ROW1.Value = Application.VLookup(GRN, rng, 3, False)
Me.textbox_COLOUM1.Value = Application.VLookup(GRN, rng, 4, False)
Me.textbox_PALLET1.Value = Application.VLookup(GRN, rng, 5, False)
End Sub
不过我更喜欢这样的东西:
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