VBA vlookup 数据不匹配

VBA vlookup data mismatch

我是 VBA 的新手,面临以下问题:

我需要 return 某个值,return根据数值数据通过 IF 公式计算,保存在另一项工作中sheet。 我写过这样的东西,但是一直到 运行 IF 部分,它都会给我错误 Type mismatch,问题似乎是在值中,由 vlookup 找到。我试图将它声明为 long、variant 等等,但这没有帮助。然而,MsgBox return 正确地处理了另一个 sheet 的结果。另一个 sheet 被格式化为数字。有什么想法可以让它发挥作用吗?

这是我现在拥有的代码:

Option Explicit
Sub find()

Dim lookup As String
Dim pkgWidth, pkgLength, pkgHeight, displaySize, AllHeaders, headerweight, itemweight, classify  As Range
Dim lastrow As Variant
Dim cl As Range
Dim i As Integer
Dim widthh, lengthh, Heightt, display, Weight As Variant

'this part dynamically searches for the columns I need
Set AllHeaders = Worksheets("Sheet2").Range("1:1")
Set pkgWidth = AllHeaders.find("package_width")
Set pkgLength = AllHeaders.find("package_length")
Set pkgHeight = AllHeaders.find("package_height")
Set displaySize = AllHeaders.find("display_size")


Set headerweight = Worksheets("Sheet1").Range("1:1")
Set itemweight = headerweight.find("Item Weight")
Set classify = headerweight.find("AT")

lastrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow

lookup = Worksheets("Sheet1").Cells(i, 1).Value
Set cl = Worksheets("Sheet1").Cells(i, classify.Column)

'here the values are being looked up from another sheet
widthh = Application.VLookup(lookup, _
Worksheets("Sheet2").Range("A1").CurrentRegion, pkgWidth.Column, False)

lengthh = Application.VLookup(lookup, _
Worksheets("Sheet2").Range("A1").CurrentRegion, pkgLength.Column, False)

Heightt = Application.VLookup(lookup, _
Worksheets("Sheet2").Range("A1").CurrentRegion, pkgHeight.Column, False)

display = Application.VLookup(lookup, _
Worksheets("Sheet2").Range("A1").CurrentRegion, displaySize.Column, False)

Weight = Application.VLookup(lookup, _
Worksheets("Sheet1").Range("A1").CurrentRegion, itemweight.Column, False)

If display > 6 Then
    If Weight < 25 Then
        cl.Value = 1.01
        Else
        cl.Value = 1.02
        End If
    Else
        If widthh >= 1970 Or lengthh >= 1970 Or Heightt >= 1970 Then
            If Weight <= 8 Then
            cl.Value = 3.01
            Else
                If Weight >= 35 Then
                cl.Value = 3.02
                Else
                cl.Value = 3.03
                End If
            End If
         Else
            If Weight <= 3 Then
            cl.Value = 5.01
            Else
                If Weight >= 8 Then
                cl.Value = 5.03
                Else
                cl.Value = 5.02
                End If
        End If
    End If
End If

Next i

End Sub

当使用 Application.VLookup(或其任何变体)时,您必须考虑到它可以 return #N/A,如 documentation 中所述:

If lookup_value is smaller than the smallest value in the first column of table_array, VLOOKUP returns the #N/A error value.

例如,如果 display 获取该值,则表达式 display > 6 会给您 类型不匹配 错误。

所以为了防止这种情况发生,要么改变你的代码逻辑,保证 VLookup 不会 return #N/A(如果这在你的情况下是可能的,我不能说), 或者测试这个错误值,像这样:

If IsError(display) Then
    ' Treat the error condition...
ElseIf display > Then
    ' ...etc.

获得 VLookup 调用结果的其他变量可能需要相同的预防措施。