我有一个附加了几个不同宏的USER FORM。
当我从用户窗体启动宏时,我得到一个结果,当我从模块中午餐了宏时,我得到了一个不同的结果。
在宏代码下面:
Sub WeekEndsCheck()
Dim cell As Range
Dim Ret As Variant
For Each cell In Worksheets("Sheet2").Range(Range("B2"), Range("B2").End(xlDown))
On Error Resume Next
Ret = Application.WorksheetFunction.VLookup(cell, _
Worksheets("DataBase").Range("A:A"), 1, 0)
On Error GoTo 0
If Ret <> "" Then
If cell = Ret Then
cell.Interior.Color = RGB(255, 0, 0)
End If
Ret = ""
End If
Next
For Each cell In Worksheets("Sheet2").Range(Range("D2"), Range("D2").End(xlDown))
On Error Resume Next
Ret = Application.WorksheetFunction.VLookup(cell, _
Worksheets("DataBase").Range("A:A"), 1, 0)
On Error GoTo 0
If Ret <> "" Then
If cell = Ret Then
cell.Interior.Color = RGB(255, 0, 0)
End If
Ret = ""
End If
Next
End Sub
我正在尝试检查VLOOK UP
是否在某个日期范围内包含另一个名为数据库的工作表中的某些日期。
我基本上是在检查我的约会日期不是周末。
我不想只检查周末,也不想按货币检查工作日。这就是为什么我创建了将复制相同代码的数据库的原因。
答案 0 :(得分:0)
这更加健壮,如果您删除WorksheetFunction,则无需进行错误处理:
Sub WeekEndsCheck()
Dim cell As Range, ws As Worksheet, rngData As Range
Set ws = Worksheets("Sheet2")
Set rngData = Worksheets("DataBase").Range("A:A")
'qualify all range() calls with a worksheet
For Each cell In ws.Range(ws.Range("B2"), ws.Range("B2").End(xlDown)).Cells
'use Match() and drop the WorksheetFunction
'Edit: when matching dates need to use CLng() to convert search date to a number
If IsError(Application.Match(CLng(cell.Value), rngData, 0)) Then cell.Interior.Color = RGB(255, 0, 0)
Next cell
For Each cell In ws.Range(ws.Range("D2"), ws.Range("D2").End(xlDown)).Cells
If IsError(Application.Match(CLng(cell.Value), rngData, 0)) Then cell.Interior.Color = RGB(255, 0, 0)
Next cell
End Sub