我尝试创建用户表单,用户可以在其中输入批号,当他们点击OK时,VLOOKUP功能将为他们提供所需的信息。但是,目标数据在两个不同的纸张上(例如,纸张1和纸张2)。
我设法在下面创建了VBA,但它仅适用于sheet1。如果输入了错误的批次,则不会显示任何结果或msgbox。
我做错了什么?
Private Sub CommandButton1_Click()
On Error GoTo myerrorhandler
Dim Batch As Long
Dim Result
Dim Result2
Dim Target1 As Range
Dim Target2 As Range
Dim ws As Worksheet
Batch = TextBox1.text
Set Target1 = ActiveWorkbook.Sheets("sheet1").Range("C7:ZZ10000")
Set Target2 = ActiveWorkbook.Sheets("sheet2").Range("C7:ZZ1000")
Result = vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 1, False)
Result = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 17, False)
Result = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 18, False)
Result = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 20, False)
If Result > 0 Then
MsgBox "Batch details:" & vbNewLine & Result
ElseIf Result = 0 Then
Result2 = vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 1, False)
Result2 = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 17, False)
Result2 = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 18, False)
Result2 = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 20, False)
If Result > 0 Then
MsgBox "Batch details:" & vbNewLine & Result2
ElseIf Result2 = 0 Then
MsgBox "Not present, or wrong entry"
End If
End If
Exit Sub
myerrorhandler:
If Err.Number = 13 Then
MsgBox "Invalid value"
End If
End Sub
答案 0 :(得分:0)
The problem was your tests in the If
, Result
is a string and the test Result > 0
isn't meaningful.
As you add 8 characters (Batch :
+ new line) at each call (4) of Result
, what you meant by Result > 0
is actually Len(Result) > 32
.
This should be closer of what you are trying to achieve :
Private Sub CommandButton1_Click()
On Error GoTo myerrorhandler
Dim Batch As Long
Dim Result
Dim Result2
Dim Target1 As Range
Dim Target2 As Range
Dim Ws As Worksheet
Batch = TextBox1.Text
Set Target1 = ActiveWorkbook.Sheets("sheet1").Range("C7:ZZ10000")
Set Target2 = ActiveWorkbook.Sheets("sheet2").Range("C7:ZZ1000")
Result = vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 1, False)
Result = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 17, False)
Result = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 18, False)
Result = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target1, 20, False)
If Len(Result) > 32 Then
MsgBox "Batch details:" & vbNewLine & Result
ElseIf Len(Result) = 32 Then
Result2 = vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 1, False)
Result2 = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 17, False)
Result2 = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 18, False)
Result2 = Result & vbNewLine & "Batch: " & Application.WorksheetFunction.VLookup(Batch, Target2, 20, False)
If Len(Result2) > 32 Then
MsgBox "Batch details:" & vbNewLine & Result2
ElseIf Len(Result2) = 32 Then
MsgBox "Not present, or wrong entry"
End If
End If
Exit Sub
myerrorhandler:
If Err.Number = 13 Then MsgBox "Invalid value"
End Sub