I am new to VBA and I've written a code which gets a start and stop date from report X and retrieves data from report Y where the dates lie between the start and the stop date. (e.g. 18 Jun 2018 > data form report Y < 05 Jul 2018).
Here comes the issue: my VBA code does not run correctly since it tells me in an if-else-statement, that 18 Jun 2018 is greater than 05 Jul 2018, which cannot be true. I have tried formatting the dates but it still would't work.
Sub calculateCompliance()
n = 0
' calculate for screening
For i = 1 To 500
If Worksheets("Sheet1").Cells(i, 6) > Worksheets("Sheet2").Range("E5") _
And Worksheets("Sheet1").Cells(i, 6) < Worksheets("Sheet2").Range("F5") _
Then
n = n + 1
End If
Next i
Worksheets("Sheet2").Range("E6").Value = n
End Sub
EDIT: By adding the variable declarations i.e. "dim startdate as Date", the errors have been resolved. This was really because of the different cell format. Thanks a lot!
Date
types. I'll bet anything you're doing aString
compare. - Comintern