I have a worksheet where column A is populated by dates. I am trying to have the macro pull all of the dates that fall within a given month (then 3 months previous, 6 months previous, etc.) it works for all of the previous months and it works for the first month unless it happens to be the last month in column A. To account for that I added the following:
[VBA]temp = InputBox("Enter date of first day of month to forecast (mm/dd/yyyy): ")
wbSrc.Sheets("Incoming").Activate
i = Range("A36000").End(xlUp).Row
'Dates of current month
sD = CDate(temp)
eD = DateSerial(Year(sD), Month(sD) + 1, 0)
eD = CDate(eD)
MsgBox (sD & vbCrLf & eD)
For count = i To 2 Step -1
cellD1 = CDate(Range("A" & count).Value)
cellD2 = CDate(Range("A" & (count + 1)).Value)
cellD3 = CDate(Range("A" & (count - 1)).Value)
If cellD1 <= eD And cellD2 > eD Then
eR = count
ElseIf cellD1 <= eD And Range("A" & (count + 1)).Value = "" Then
eR = count
ElseIf cellD3 < sD And cellD1 >= sD Then
sR = count
End If
Next count
Range("A" & sR & ":A" & eR).Select
Selection.Copy
ThisWorkbook.Sheets(1).Activate
Range("M1").Select
ActiveSheet.Paste[/VBA]
The dates of current months work, and the whole code works if the date is not followed by an empty row but almost always with this report the date to forecast will be the last 20 or so rows of column A.