Excel &引用;类型不匹配错误“;用VLookup
如果first_unit=“N/a”然后,则该行出现类型不匹配错误。我试图根据另一个下拉菜单(B10)中的选择更改下拉菜单(B26:C26)的文本。对于以下代码:Excel &引用;类型不匹配错误“;用VLookup,excel,vba,variant,mismatch,Excel,Vba,Variant,Mismatch,如果first_unit=“N/a”然后,则该行出现类型不匹配错误。我试图根据另一个下拉菜单(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
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
Vlookup中的第一个参数是查找值,而不是范围,我认为它是“B10”而不是范围(“B10:E10”) 您需要对Vlookup使用错误处理 通过“错误时继续下一步”而不是错误时转到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
Vlookup中的第一个参数是查找值,而不是范围,我认为它是“B10”而不是范围(“B10:E10”) 您需要对Vlookup使用错误处理 通过“错误时继续下一步”而不是错误时转到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
使用
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
使用
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
N/A或#N/A???见;请注意
Application.WorksheetFunction.VLookup()
和Application.VLookup()
N/A或#N/A???之间的区别,请参阅;请注意Application.WorksheetFunction.VLookup()
和Application.VLookup()
之间的区别您的解释不清楚。第一个参数是查找值,而不是范围。Excel将单元格转换为函数中的值。因此,为了清楚起见,您应该使用Range(“B10”).Value
作为第一个参数。你是对的,你不能用多单元格范围来做这件事。@D_bester你为什么否决我的答案?即使我对你的代码进行了适当的审查,你的解释还是不清楚。第一个参数是查找值,而不是范围。Excel将单元格转换为函数中的值。因此,为了清楚起见,您应该使用Range(“B10”).Value
作为第一个参数。你是对的,你不能用多单元格范围来做这件事。@D_bester你为什么否决我的答案?即使在我对您的代码进行了适当的审阅之后?如果这个答案对您有帮助,请勾选左侧的复选标记,接受它。如果不起作用,请告诉我。如果这个答案有助于你,请勾选左边的复选标记,接受它。如果不行,请告诉我。