【发布时间】:2020-02-20 13:49:56
【问题描述】:
我正在寻找一种方法来重复搜索包含事件的日期表。
用户将选择开始日期和结束日期,我需要知道这些日期是否包含事件。
工作表列出了结束日期和开始日期之间的所有日期。我需要搜索这个数组。
前端视图
我希望宏在 A 列中搜索列表中的日期,并在任何日期对应于 E 列中的事件时返回一个 msgbox。
这是我目前所拥有的。我被困在如何将 SearchDate 作为我的 vlookup 的变量范围,以及如何在找到一个结果后停止循环,因为这足以提示警告消息。
Sub EventFinder()
Dim RowNMBR As Long
Dim SearchDate As Range
RowNMBR = 4
Set SearchDate = Cells(4, 12)
With SearchDate
For Each c In Range("L5:L33")
On Error Resume Next
RowNMBR = RowNMBR + 1
Set SearchDate = Cells(RowNMBR, 12)
If Not Application.WorksheetFunction.VLookup(SearchDate, Sheets("Forecast").Range("A:E"), 5, False) = "" _
Then MsgBox "There is an Event on these dates, contact the Revenue Manager!", vbOKOnly, "Event Warning"
Exit Sub ' and exit procedure
Next c
On Error GoTo 0
End With
End Sub
为了添加到宏中,我创建了一个自动宏,以便在“DoA”或“Nights”的值发生变化时调用我的宏。这不能正常工作。
只要我在处理工作表和工作簿,我就解除了保护,但它仍然无法工作。
问题已通过以下代码修复
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim intersection As Range
' Target => you already have an address of changed cell(s)
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("E6")
' Application.Intersect - returns a Range object that represents the
' rectangular intersection of two or more ranges.
Set intersection = Application.Intersect(KeyCells, Target) ' if it intersects that the range will be initialized
If Not (Target.Rows.Count > 1 And Target.Columns.Count > 1) Then ' check that changed range has only 1 cell
' because if you select a 6th row
' and clear it's contents (or change in any other way) -
' the event will be triggered as well
If Not intersection Is Nothing Then ' if the intersection range is initialized
' then event will be triggered
Call EventFinder
End If
End If
Set KeyCells = Range("E9")
' Application.Intersect - returns a Range object that represents the
' rectangular intersection of two or more ranges.
Set intersection = Application.Intersect(KeyCells, Target) ' if it intersects that the range will be initialized
If Not (Target.Rows.Count > 1 And Target.Columns.Count > 1) Then ' check that changed range has only 1 cell
' because if you select a 6th row
' and clear it's contents (or change in any other way) -
' the event will be triggered as well
If Not intersection Is Nothing Then ' if the intersection range is initialized
' then event will be triggered
Call EventFinder
End If
End If
Set KeyCells = Range("E12")
' Application.Intersect - returns a Range object that represents the
' rectangular intersection of two or more ranges.
Set intersection = Application.Intersect(KeyCells, Target) ' if it intersects that the range will be initialized
If Not (Target.Rows.Count > 1 And Target.Columns.Count > 1) Then ' check that changed range has only 1 cell
' because if you select a 6th row
' and clear it's contents (or change in any other way) -
' the event will be triggered as well
If Not intersection Is Nothing Then ' if the intersection range is initialized
' then event will be triggered
Call EventFinder
End If
End If
End Sub
【问题讨论】: