I am looking for a way to repeatedly search through a date table with events.
The user will select a start date and an end date and I need to know if any of these dates contain an event.
The worksheet lists all the dates between the end date and the start date. I need to search through this array.
Front end view
The search area is a table in another sheet in the workbook looking like this:
Date Table
I want the macro to search through column A for the dates in the list and return a msgbox if any of the dates correspond to an event in column E.
This is what I have so far. I am stuck on how to have SearchDate as a variable range for my vlookup, and also how to stop the loop once it has found one result, as this will be enough to prompt the warning message.
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
To add to the macro I created an automated macro to call my macro whenever the value of "DoA" or "Nights" changes. This does not work as it should.
I unprotected the sheets and workbook for as long as I am working on it and it still does not work.
PROBLEM IS FIXED WITH CODE BELOW
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