Excel Ninja In Training
New Member
- Joined
- May 6, 2011
- Messages
- 25
I am trying to compare the date in an active cell to a reference date in cell U1, using:
If "U1" > (ActiveCell) Then
I've got a loop that is evaluating this in turn for each cell in the B column. Sadly, Excel is always interpreting the above statement to be true, even when U1< the active cell (i.e. the active cell is more recent than the reference date).
Here's the full code:
As a reference, to make sure that I'm doing things correctly, I entered the formula "=U$1>B2" into cell C2 and filled down the whole C column. This correctly gave me "true" when the corresponding B cell was older than the reference date and "false" when it was newer, so the code above is my effort to duplicate that formula in VBA code.
Can anyone tell me what I'm doing wrong?
If "U1" > (ActiveCell) Then
I've got a loop that is evaluating this in turn for each cell in the B column. Sadly, Excel is always interpreting the above statement to be true, even when U1< the active cell (i.e. the active cell is more recent than the reference date).
Here's the full code:
Sub test4()
Sheets("Consolidate").Select
Range("B2").Select
Do
If "U1" > (ActiveCell) Then
ActiveCell.EntireRow.Delete
Else: ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell.Offset(0, 1))
End Sub
When I run this code, Excel considers each row and then deletes it, regardless of whether the U1 is greater than or less than (i.e. newer than or older than) the active cell.Sheets("Consolidate").Select
Range("B2").Select
Do
If "U1" > (ActiveCell) Then
ActiveCell.EntireRow.Delete
Else: ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell.Offset(0, 1))
End Sub
As a reference, to make sure that I'm doing things correctly, I entered the formula "=U$1>B2" into cell C2 and filled down the whole C column. This correctly gave me "true" when the corresponding B cell was older than the reference date and "false" when it was newer, so the code above is my effort to duplicate that formula in VBA code.
Can anyone tell me what I'm doing wrong?