Excel VBA Vlookup返回错误的结果
我目前正在尝试制作一个应用程序,在使用VBA的同时,我可以在单个工作表中存储销售信息 当我尝试使用Vlookup来确定ProductID的价格时,我不必在自己身上输入值,Vlookup总是返回相同的值“2015” 我不知道哪里出了问题 这是图纸的布局: 这是我的用户表单的布局: 这是我在命令按钮上使用的代码:Excel VBA Vlookup返回错误的结果,excel,vba,vlookup,Excel,Vba,Vlookup,我目前正在尝试制作一个应用程序,在使用VBA的同时,我可以在单个工作表中存储销售信息 当我尝试使用Vlookup来确定ProductID的价格时,我不必在自己身上输入值,Vlookup总是返回相同的值“2015” 我不知道哪里出了问题 这是图纸的布局: 这是我的用户表单的布局: 这是我在命令按钮上使用的代码: Private Sub CommandButton1_Click() Dim emptyRow As Long Dim r As Range Dim Productprijs As I
Private Sub CommandButton1_Click()
Dim emptyRow As Long
Dim r As Range
Dim Productprijs As Integer
Dim productaantal As Integer
Dim Eindprijs As Integer
Sheet1.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = TextBox1.Value
Cells(emptyRow, 2).Value = TextBox2.Value
Cells(emptyRow, 3).Value = TextBox3.Value
Cells(emptyRow, 5).Value = TextBox4.Value
Productprijs = CInt(Application.VLookup(TextBox3.Value, "J2:L2000", 3, False))
productaantal = TextBox2.Value
Eindprijs = Productprijs * productaantal
Cells(emptyRow, 4).Value = Eindprijs
UserForm1.Hide
有人能帮我解决这个问题吗?这可能只是我目前忽略的一件小事
谢谢你,
Martijn您的代码有两个问题; “J2:L2000”应替换为范围(“J2:L2000”)(2015是2015错误的整数版本) 如果Vlookup找不到Textbox3,那么代码将无法工作。值:在这种情况下,它将返回一个错误:代码应该更像这样
Sub testv()
Dim v As Variant
Dim i As Long
v = Application.VLookup(9, Range("A1:a3"), 1, False)
If Not IsError(v) Then
i = CLng(v)
Else
MsgBox "not Found"
End If
End Sub
你的代码有两个问题; “J2:L2000”应替换为范围(“J2:L2000”)(2015是2015错误的整数版本) 如果Vlookup找不到Textbox3,那么代码将无法工作。值:在这种情况下,它将返回一个错误:代码应该更像这样
Sub testv()
Dim v As Variant
Dim i As Long
v = Application.VLookup(9, Range("A1:a3"), 1, False)
If Not IsError(v) Then
i = CLng(v)
Else
MsgBox "not Found"
End If
End Sub
您的代码正在抛出一个“2015错误”,因为您将TextBox3.Value
作为第一个参数放入VLookup
函数中。请注意,以下代码有效:
Private Sub CommandButton1_Click()
Dim emptyRow As Long
Dim Price As Variant
Dim Quantity As Double
Dim Total As Double
Dim x As Double
'This finds the next empty row in the first table
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Place the new values from the UserForm to the table
Cells(emptyRow, 1).Value = TextBox1.Value 'Date
Cells(emptyRow, 2).Value = TextBox2.Value 'Quantity
Cells(emptyRow, 3).Value = TextBox3.Value 'ProductID
Cells(emptyRow, 5).Value = TextBox4.Value 'Customer
'Assign the value of the ProductID text box to x
x = TextBox3.Value
'Calculate the total price, using x instead of TextBox3.Value
Price = Application.VLookup(x, Range("J2:L3"), 3, False)
Quantity = TextBox2.Value
Total = Price * Quantity
Cells(emptyRow, 4).Value = Total
UserForm1.Hide
End Sub
这也消除了使用CInt
转换价格变量的需要。希望其他人能清楚地解释为什么VLookup
中的TextBox3.Value
会抛出错误?您的代码会抛出“2015错误”,因为您将TextBox3.Value
作为第一个参数放在VLookup
函数中。请注意,以下代码有效:
Private Sub CommandButton1_Click()
Dim emptyRow As Long
Dim Price As Variant
Dim Quantity As Double
Dim Total As Double
Dim x As Double
'This finds the next empty row in the first table
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Place the new values from the UserForm to the table
Cells(emptyRow, 1).Value = TextBox1.Value 'Date
Cells(emptyRow, 2).Value = TextBox2.Value 'Quantity
Cells(emptyRow, 3).Value = TextBox3.Value 'ProductID
Cells(emptyRow, 5).Value = TextBox4.Value 'Customer
'Assign the value of the ProductID text box to x
x = TextBox3.Value
'Calculate the total price, using x instead of TextBox3.Value
Price = Application.VLookup(x, Range("J2:L3"), 3, False)
Quantity = TextBox2.Value
Total = Price * Quantity
Cells(emptyRow, 4).Value = Total
UserForm1.Hide
End Sub
这也消除了使用
CInt
转换价格变量的需要。希望其他人能够清楚地解释为什么VLookup
中的TextBox3.Value
会抛出错误?尝试将Productprijs=CInt(Application.VLookup(TextBox3.Value,“J2:L2000”,3,False))
替换为Productprijs=CInt(范围(“L”&Application.Match(TextBox3.Value,范围(“J”),0)))
尝试将Productprijs=CInt(Application.VLookup(TextBox3.Value,“J2:L2000”,3,False))
替换为Productprijs=CInt(Range(“L”)和Application.Match(TextBox3.Value,Range(“J:J”),0))