如何在我的VLOOKUP上修复错误类型13?

问题描述 投票:0回答:1

我在Excel上有此VBA代码。当我执行它时,出现错误“ Erreur 13:Incompatibilitéde type”。

Vlookup在两个工作簿之间应用。

[Range("A2:232")包含日期类型,也包含Cells(i,Lcol-4)

请帮助。

这是我的代码

    If Application.Vlookup(Cells(i,Lcol-4), Workbooks("V_TAUX.xlsx").Sheets(1).Range("A2:232"),1,False) = Cells(i,Lcol-4) Then
        Instruction...
        Instruction....
    End If
vba excel-vba
1个回答
0
投票

[Cells(i,Lcol-4)或等效的Cells(i,Lcol-4).Value返回日期类型值,而工作表中列出的日期实际上是格式为日期的序列号。

结果,VLookup将无法返回比赛。而且,由于您已经使用Application而不是WorksheetFunction来限定了引用,因此它将返回一个不间断的错误。

但是,由于正在将VLookup返回的错误与Cells(i,Lcol-4)中的值进行比较,因此会出现类型不匹配中断错误。

所以,首先我建议您将查找值转换为Long数据类型...

 Application.Vlookup(CLng(Cells(i,Lcol-4).Value) . . .

然后,如Chronocidal所建议的,将返回值分配给Variant,以便您可以使用IsError检查错误...

Dim matchVal As Variant

matchVal = Application.VLookup(CLng(Cells(i, Lcol - 4).Value), Workbooks("V_TAUX.xlsx").Sheets(1).Range("A2:A232"), 1, False)

If Not IsError(matchVal) Then
    'etc
    '
    '
End If

尽管,由于您只是检查匹配项,因此可以改用Application.Match ...

Dim matchVal As Variant

matchVal = Application.Match(CLng(Cells(i, Lcol - 4).Value), Workbooks("V_TAUX.xlsx").Sheets(1).Range("A2:A232"), 0)

If Not IsError(matchVal) Then
    'etc
    '
    '
End If
© www.soinside.com 2019 - 2024. All rights reserved.