VBA - Greater than or equal to not working with dates.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
Hopefully a quick one. I'm referencing a date (31/12/2018) as a variable "date1"

I'm then going onto another sheet and saying if the value in B is >= date1 and <= date2 then do this.

When I set it to 31/12/2018, even though this cell is less than the value in B (which is 07/01/2019), it doesn't work. However, if I change date1 to be 01/01/2019 it does work.

Somehow 01/01/2019 is valid as being less than 07/01/2019, but 31/12/2018 isn't working.

Any ideas?

Thanks.
 
You are actually NOT declaring those variables as dates.
Look at your DIM line at the top.
Code:
... [COLOR=#333333]date1 As [/COLOR][COLOR=#ff0000]String[/COLOR][COLOR=#333333], date2 As [/COLOR][COLOR=#ff0000]String[/COLOR][COLOR=#333333] ...[/COLOR]
Change that to "As Date" for both of them.


Ah, I see.

Would this have an effect where, later on in the code we have things like "name = date 1 & wbyr & "Commission" - so where multiple things are concatenated together?
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
So, you can store numbers/dates in a string field, but then become a string, and you cannot do things like date math on them. That is what is happening to you.

Would this have an effect where, later on in the code we have things like "name = date 1 & wbyr & "Commission" - so where multiple things are concatenated together?
You can concatenate variables of different types together. That is not an issue. However, if you want the date part to appear in a particular format, you will want to apply the VBA Format function to it (looks pretty much the same as the Excel Text function).

So, you may have something like this:
Code:
[COLOR=#333333]name = FORMAT(date1,"yyyy/mm/dd") & wbyr & " Commission"[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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