【问题标题】:"Type mismatch error" with VLookupVLookup 出现“类型不匹配错误”
【发布时间】:2016-01-23 14:23:35
【问题描述】:

我收到If first_unit = "N/A" Then 行的类型不匹配错误。我正在尝试根据另一个下拉菜单 (B10) 中的选择来更改下拉菜单 (B26:C26) 的文本。对于以下代码:

Dim check_change As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo 0


If check_change = False Then

If Target.Address = Range("B10").Address Then
    Dim first_unit As Variant
    Dim second_unit As Variant
    Dim third_unit As Variant

    check_change = True
    first_unit = Application.WorksheetFunction.VLookup(Range("B10:E10"), Sheet3.Range("Jurisdictions_table"), 5, False)
    second_unit = Application.WorksheetFunction.VLookup(Range("B10:E10"), Sheet3.Range("Jurisdictions_table"), 6, False)
    third_unit = Application.WorksheetFunction.VLookup(Range("B10:E10"), Sheet3.Range("Jurisdictions_table"), 7, False)

    Range("D5").Value = first_unit
    Range("E5").Value = second_unit
    Range("F5").Value = third_unit

    If first_unit = "N/A" Then
        Range("B26:C26").Value = "Certified"
    End If

    check_change = False
    Exit Sub
End If

If Not Intersect(Target, Range("B19")) Is Nothing Then
    check_change = True
    Call ft_to_m(Range("D19"), Range("B19"))
    check_change = False
    Exit Sub
End If

If Not Intersect(Target, Range("D19")) Is Nothing Then
    check_change = True
    Call m_to_ft(Range("D19"), Range("B19"))
    check_change = False
    Exit Sub
End If

End If

End Sub

【问题讨论】:

标签: excel vba variant mismatch


【解决方案1】:

Vlookup 中的第一个参数是查找值,不是 Range,我认为是 "B10" 而不是 Range("B10:E10")

您需要对 Vlookup 使用错误处理 ' 通过 'On Error Resume Next' 而不是 on error Goto 0 启用错误处理

Sub test1()
On Error Resume Next 

    first_unit = Application.WorksheetFunction.VLookup(Range("B10").value, Sheet3.Range("Jurisdictions_table"), 5, False)
        'first_unit = Application.WorksheetFunction.VLookup(Range("B10:E10"), Sheet3.Range("Jurisdictions_table"), 5, False )

    If IsError(first_unit) = False Then
    ' Example
    first_unit = "N/A"

    Else

    first_unit = "Otherwise"

    End If

End Sub

【讨论】:

  • 你的解释不清楚。第一个参数是查找值,而不是范围。 Excel 将单元格转换为函数中的值。所以要清楚你应该使用Range("B10").Value 作为第一个参数。你是对的,你不能为此使用多单元格范围。
  • @D_bester 为什么你不赞成我的回答?即使我对您的代码进行了适当的审查?
【解决方案2】:

使用Application.VLookup 而不是Application.WorksheetFunction.VLookup() 以便在查找失败时不会引发错误。然后您可以检查IsError() 以确定您的结果是否有效或错误。在 Excel 中,如果在查找表中找不到该值,则会显示 #N/A 错误。使用IsError() 将捕获错误。

第一个参数也是查找值,而不是范围。 Excel 将单元格转换为函数中的值。所以要清楚你应该使用Range("B10").Value 作为第一个参数。而且您不能为此使用多单元格范围。

first_unit = Application.VLookup(Range("B10").Value, Sheet3.Range("Jurisdictions_table"), 5, False)
second_unit = Application.VLookup(Range("C10").Value, Sheet3.Range("Jurisdictions_table"), 6, False)
third_unit = Application.VLookup(Range("D10").Value, Sheet3.Range("Jurisdictions_table"), 7, False)

If Not IsError(first_unit) Then Range("D5").Value = first_unit
If Not IsError(second_unit) Then Range("E5").Value = second_unit
If Not IsError(third_unit) Then Range("F5").Value = third_unit

If IsError(first_unit) Then
    Range("B26:C26").Value = "Certified"
End If

CPearson: Error Handling With Worksheet Functions

【讨论】:

  • 如果此答案对您有帮助,请勾选左侧的复选标记接受。如果它不起作用,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
相关资源
最近更新 更多