简体   繁体   English

使用Vlookup函数时出现运行时错误1004

[英]Run time error 1004 on using Vlookup function

Im trying to highlight range of cells when a date is present in the list of holidays. 我试图在假期列表中显示日期时突出显示单元格范围。 But on running the below code, the Run time error 1004 is displayed. 但是在运行以下代码时,将显示运行时错误1004。 I have even tried handling it in error handler; 我什至尝试在错误处理程序中处理它; but it is not working. 但它不起作用。 Can somebody please help me why this error is occuring and resolve the same? 有人可以帮我为什么会发生此错误并解决该错误吗?

Sub highlight_cells()
Dim myrange As Range
On Error GoTo myerr:
For i = 1 To 10
Set myrange = Range(Cells(1, i), Cells(10, i))
temp = Application.WorksheetFunction.VLookup(Range(Cells(1, i)), [holidays], 2, False)
If (Application.WorksheetFunction.IsNA(temp)) Then
myrange.Interior.Color = 3
End If
Next i

myerr:
If Err.Number = 1004 Then
MsgBox "vlookup error"
End If
End Sub

Range(Cells(1, i)) isn't a valid range reference Range(Cells(1, i))不是有效的范围引用

maybe you wanted to reference Cells(1, i) 也许您想引用Cells(1, i)

furthermore you can exploit the Application VLookup() method that wraps the possible error in the returned variant variable that you can check with IsError() function like follows: 此外,您可以利用Application VLookup()方法,该方法将可能的错误包装在返回的变量中,可以使用IsError()函数进行检查,如下所示:

Dim temp As Variant
For i = 1 To 10
    Set myrange = Range(Cells(1, i), Cells(10, i))
    temp = Application.VLookup(Cells(1, i), [holidays], 2, False)
    If Not IsError(temp) Then Cells(1, i).Interior.Color = 3
Next i

Here is a conditional formatting method, without using VBA. 这是不使用VBA的条件格式设置方法。

Select your range > Conditional Formating > New Rule > Use a formula ... 选择范围>条件格式>新规则>使用公式...

Enter this formula 输入此公式

=VLOOKUP($A2,$J$2:$K$6,1,FALSE)

Take care of the "$" in the formula. 注意公式中的“ $”。 This should highlight all cells that were found in the holidays list. 这应该突出显示在假期列表中找到的所有单元格。

条件格式V查找

Your code is okay , It worked in Excel 2010 , Your problem is with VBA Error handling method. 您的代码还可以,它在Excel 2010中可用,您的问题是VBA错误处理方法。

Go to Tools --> Options -->General --> Error Trapping And check "Break on unhanded Errors" 转到工具->选项->常规->错误陷阱,然后选中“打破未处理的错误”

在此处输入图片说明

sorry all these times I was referring to column 2 in vlookup. 很抱歉,我一直在指vlookup中的第2列。 That was causing the problem. 那就是问题所在。 The list of holiday is a single column list. 假期列表是一个单列列表。 Hence vlookup was throwing error. 因此,vlookup抛出错误。 ANd one more thing the named ranges work as I have entered and even the actual range also gives the same result. 以及我输入的命名范围可以正常工作的另一件事,甚至实际范围也给出了相同的结果。

Sub highlight_cells() Dim myrange As Range 子Highlight_cells()昏暗的myrange作为范围

For i = 1 To 10 对于i = 1到10

Set myrange = Range(Cells(1, i), Cells(10, i)) MsgBox Cells(1, i) temp = Application.VLookup(Cells(1, i), [holidays], 1, False) If Not IsError(temp) Then myrange.Interior.ColorIndex = 3 End If 设置myrange = Range(Cells(1,i),Cells(10,i))MsgBox Cells(1,i)temp = Application.VLookup(Cells(1,i),[holidays],1,False)如果不是IsError (temp)然后myrange.Interior.ColorIndex = 3如果结束

Next i 接下来我

End Sub 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM