我的VLookup功能有问题,因为它找不到我知道的值在该范围内。 Product is变量必须保持字符串,因为我正在查找数字和带文本的混合数字。我的导师也不允许使用变体。
Sub LookupValue()
Dim Product As String
Dim ErrCheck As Boolean
Dim Quantity As Integer
Dim Discount As Double
Dim myRange As Range
Set myRange = Worksheets("Prices").Range("A2:C21")
ErrCheck = True
'Obtaining VLookup Value
Product = InputBox("Enter the product's code.")
'Error checking
Do Until ErrCheck = False
If Product = "" Then
ErrCheck = True
MsgBox ("Not a valid entry.")
Product = InputBox("Enter the product's code.")
ElseIf IsError(Application.VLookup(Product, myRange, 3, False)) Then
ErrCheck = True
MsgBox ("The value entered was not found.")
Product = InputBox("Enter the product's code.")
Else
ErrCheck = False
End If
Loop
'Obtaining Quantity Value
Quantity = InputBox("Enter the quantity ordered.")
'Error checking
Do Until ErrCheck = False
If IsNumeric(Quantity) = False Then
ErrCheck = True
MsgBox ("Not a valid entry.")
Quantity = InputBox("Enter the quantity ordered.")
Else
ErrCheck = False
End If
Loop
'Obtaining discount rate
If Quantity < 25 Then
Discount = 0.1
If Quantity < 50 Then
Discount = 0.15
If Quantity < 75 Then
Discount = 0.2
If Quantity < 100 Then
Discount = 0.25
If Quantity >= 100 Then
Discount = 0.3
End If
End If
End If
End If
End If
'Filling in cells
Sales.Range("B2") = Product
Sales.Range("B3") = Application.VLookup(Product, myRange, 2, False)
Sales.Range("B4") = Quantity
Sales.Range("B5") = Discount
Sales.Range("B6") = Application.VLookup(Product, myRange, 3, False)
Sales.Range("B7") = Range("B6").Value * Quantity
Sales.Range("B8") = Range("B7").Value * Discount
Sales.Range("B9") = Application.WorksheetFunction.Sum("B7:B8")
End Sub
查找范围的一个例子是
89044 |小工具| 12.00
答案 0 :(得分:2)
您检查的数据是类型编号,但是,您为VLOOKUP函数提供了一个字符串。
因此,请使用
代替Application.VLookup(Product, myRange, 3, False)
Application.VLookup(Val(Product), myRange, 3, False)