Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VLookup的VBA问题_Vba_Excel - Fatal编程技术网

VLookup的VBA问题

VLookup的VBA问题,vba,excel,Vba,Excel,我的VLookup函数有一个问题,因为它没有找到一个我知道在这个范围内的值。当我查找数字和带文本的混合数字时,Product变量必须保持为字符串。此外,我的讲师不允许使用变体 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("Pri

我的VLookup函数有一个问题,因为它没有找到一个我知道在这个范围内的值。当我查找数字和带文本的混合数字时,Product变量必须保持为字符串。此外,我的讲师不允许使用变体

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
Sub LookupValue()
将产品变暗为字符串
Dim ErrCheck作为布尔值
将数量设置为整数
双倍折扣
将myRange变暗为Range
设置myRange=工作表(“价格”)。范围(“A2:C21”)
ErrCheck=True
'获取VLookup值
Product=InputBox(“输入产品代码”)
'错误检查
直到ErrCheck=False为止
如果Product=”“,则
ErrCheck=True
MsgBox(“不是有效条目”)
Product=InputBox(“输入产品代码”)
ElseIf-IsError(Application.VLookup(Product,myRange,3,False))然后
ErrCheck=True
MsgBox(“未找到输入的值”)
Product=InputBox(“输入产品代码”)
其他的
ErrCheck=False
如果结束
环
“获取数量值”
数量=输入框(“输入订购数量”)
'错误检查
直到ErrCheck=False为止
如果IsNumeric(Quantity)=False,则
ErrCheck=True
MsgBox(“不是有效条目”)
数量=输入框(“输入订购数量”)
其他的
ErrCheck=False
如果结束
环
获得贴现率
如果数量小于25,则
折扣=0.1
如果数量小于50,则
折扣=0.15
如果数量小于75,则
折扣=0.2
如果数量小于100,则
折扣=0.25
如果数量>=100,则
折扣=0.3
如果结束
如果结束
如果结束
如果结束
如果结束
“填充细胞
销售范围(“B2”)=产品
Sales.Range(“B3”)=Application.VLookup(Product,myRange,2,False)
销售范围(“B4”)=数量
销售范围(“B5”)=折扣
Sales.Range(“B6”)=Application.VLookup(Product,myRange,3,False)
销售范围(“B7”)=范围(“B6”)。价值*数量
销售范围(“B8”)=范围(“B7”)。价值*折扣
Sales.Range(“B9”)=Application.WorksheetFunction.Sum(“B7:B8”)
端接头
查找范围的一个示例是


89044 | Widget | 12.00

您检查的数据类型为number,但是,您为VLOOKUP函数提供了一个字符串

因此,不要使用
Application.VLookup(Product,myRange,3,False)
,而是使用

Application.VLookup(Val(Product), myRange, 3, False)

您确定该值不包含额外的空格或类似的内容吗?Excel中的vlookup是否返回正确的结果?是的,我确信这两个值是相同的。正在查找的值也是通用格式。请尝试一下
Application.VLookup(Val(Product),myRange,3,False)
!我以为我试过了,但很明显我换了别的东西,没有再尝试。谢谢你的帮助。这适用于数值;但是,当我输入另一个值,如“45-C-33”时,找不到它。我已经实现了一个isnumeric,用于介于Val(Product)和just Product之间。谢谢你的回答。