VBA comparison operator not working

JessWhyCuh

New Member
Joined
Jul 10, 2018
Messages
15
Hey all,

A little background on my vba project: When the date in a cell is a past date, the cell style changes to "Bad".
I am trying to code this logic: If you change a past date to today's or a future date, then change a cell in another column, but same row, to the style "Neutral". For some reason this line of code only works with the <, <=, or = operator. It does not work with > or >= operators which is what I need. Why? Or if you know a better way to code this please share because I don't understand why it doesn't work this way. Thank you! Down below is my code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Style = "Bad" And Target.Value>= Date Then Cells(Target.Row, "W").Style = "Neutral"
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
That should work. I suggest you step through the code and check that the value and style are what you think they are.
 
Upvote 0
Hello JessWhyCuh,

The problem is with the event firing sequence. The Change event does not fire until after the change to cell has been made. To make this work you have to save the value before the change happens. This can be done using a Worksheet Selection-Change event and a worksheet level variable to save the cell value before it changes.

Be sure to erase any previous Change?Selection_Change event code before you copy and paste this code into your worksheet module.

Code:
Dim PrevDate As Date


Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
        If IsDate(Target) Then
            If Target >= Date Then Target.Style = "Normal" Else Target.Style = "Bad"
            Target.NumberFormat = "m/dd/yyyy"
        End If
    Application.EnableEvents = True
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If IsDate(Target) = True Then PrevDate = Target.Value
End Sub

How to Paste a Macro into a Worksheet module

  • Copy the macro with Ctrl+C.
  • Right Click the Sheet Tab at the bottom of the worksheet.
  • Click View Code
  • Paste the macro into the module with Ctrl+V.
  • Save the macro using Ctrl+S
 
Upvote 0
I added a breakpoint and stepped through it. It's giving me the correct values.

I changed the statement to an Or instead of an And which fixed the problem. However, now it will change the cell style to neutral if I enter a past date which is not on the agenda :/ Could you explain why that is? If not, that's alright because I can't seem to figure it out.

So future dates, i.e. tomorrow, are categorized with the style "Good". When I enter tomorrow's date, the Date value is today's and the Target.Style value is "Good", could this be why the And statement does not work?

P.S. I am pretty new to VBA
 
Upvote 0
Leith Ross,

Thanks so much! However, there's a typo -- in the first If statement IsDate should be IsDate(PrevDate) not (Target) :)
 
Upvote 0
Hello Jess,

Nice catch on my macro. I had to leave suddenly and wanted to get the code posted for you before I left. Thanks for posting the correction.
 
Upvote 0
Actually there's a problem :( I only want the cell to change to the style "Neutral" if it is a past date, i.e. yesterday. The code will change the style for past, present, and future. I'm not sure how to code it for if the cell contained a date in the past.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top