I'm writing a macro to find out if a given month/year falls within a date range.
The spreadsheet has 2 columns (3 and 4) which hold a start date and stop date. The macro needs to cycle through each month of the year (over 3 years) and count the rows that the given month falls within the date range
The row gets counted even if it's only for 1 day.
Example, if start date is 1/31/06, it would still get counted when checking for rows in the month of January 2006, same goes for stop dates.
This is the code I have now:
Dim row, Month, Year, count As Integer
Dim Date1, Date2 As Date
Dim Completion As Date
Date1 = #1/31/2004#
Date2 = #1/1/2004#
row = 2 'starts at 2 due to the header row
Month = 1
Year = 1
count = 0
Do While Date1 < Now()
Do While Cells(row, 1) <> ""
Completion = Cells(row, 4)
If Cells(row, 4) = "" Then
Completion = Now
End If
If Cells(row, 3) <= Date1 And Completion >= Date2 Then
count = count + 1
End If
row = row + 1
Loop
Cells(Month + 6, Year + 7) = count
count = 0
row = 2
Date1 = DateAdd("M", 1, Date1)
Date2 = DateAdd("M", 1, Date2)
If Month = 12 Then
Month = 1
Year = Year + 1
Else
Month = Month + 1
End If
Loop
The stop date might be blank (if the job is still outstanding), so I just assigned it todays date since the macro only checks up through the previous month.
The Month and Year variables are used to place the results in a table correctly.
I'm wondering if I have my IF statement correct to validate if the date range falls within the given month. I'd try to validate it using just a specific month/year, but there's thousands of rows, so manual counting is not an option and if my IF statement is incorrect, then I can't write macro to check just a single month.
Maybe there's an easier function that can do this?
Anyone have any suggestions?
Bookmarks