Excel 使用Vlookup和Table作为数据提取宏的范围时出错
消息框应显示: 零件号:(数据提取) CV或VA:“ 直达船:“ 等等 数据从一张单独的表格中提取,而不是从宏按钮中提取。 该按钮仅打开userformExcel 使用Vlookup和Table作为数据提取宏的范围时出错,excel,vba,vlookup,Excel,Vba,Vlookup,消息框应显示: 零件号:(数据提取) CV或VA:“ 直达船:“ 等等 数据从一张单独的表格中提取,而不是从宏按钮中提取。 该按钮仅打开userform 正在寻求帮助。将表用作范围的原因是,此数据将定期更新,而命名的动态范围不起作用。稍微简化一点:我不了解您在此处使用的偏移量,因此您可能需要调整一些列编号 Private Sub Search_Click() On Error GoTo MyErrorHandler: Dim PartNumber As String Dim det A
正在寻求帮助。将表用作范围的原因是,此数据将定期更新,而命名的动态范围不起作用。稍微简化一点:我不了解您在此处使用的偏移量,因此您可能需要调整一些列编号
Private Sub Search_Click()
On Error GoTo MyErrorHandler:
Dim PartNumber As String
Dim det As String
PartNumber = PartNumberIN.Text ' take part input from textbox
det = "Part Number: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 2), 3, False)
det = det & vbNewLine & "Part Description: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 4), 5, False)
det = det & vbNewLine & "CV or VA: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 1), 2, False)
det = det & vbNewLine & "Direct Ship?: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 3), 4, False)
det = det & vbNewLine & "Storage Container: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 6), 7, False)
det = det & vbNewLine & "SAP Location: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 13), 14, False)
det = det & vbNewLine & "Physical Location: " & Application.WorksheetFunction.VLookup(PartNumber, Application.WorksheetFunction.Offset(Table1, 0, 14), 15, False)
MsgBox "Part Details : " & vbNewLine & det
Exit Sub
MyErrorHandler:
If Err.Number = 1004 Then
MsgBox "Part not present in table."
ElseIf Err.Number = 13 Then
MsgBox "You have entered an invalid value"
ElseIf Err.Number = 429 Then
MsgBox "You're toast"
End If
很抱歉,编码块未能正确上传。表1的定义在哪里?非常感谢!
Private Sub Search_Click()
Dim PartNumber As String
Dim det As String, m, rng
'get the table's data range
Set rng = ThisWorkbook.Sheets("Sheet1").ListObjects("table1").DataBodyRange
'Debug.Print rng.Address
PartNumber = PartNumberIN.Text ' take part input from textbox
'locate the row if there's a match in the first column
m = Application.Match(PartNumber, rng.Columns(1), 0)
If Not IsError(m) Then
'got a match, so construct the message
With rng.Rows(m)
det = "Part Number: " & PartNumber
det = det & vbNewLine & .Cells(5).Value
'etc...
MsgBox "Part Details : " & vbNewLine & det
End With
Else
'no match
MsgBox "Part not present in table."
End If
End Sub