Visual Basic/Excel | Condition with "Date"

Solved
Marounda -  
 Marounda -
Hello,

I created a calculation tool for my work.

It's pretty good, and I would like to protect it, but I'm stuck on something really simple.

It all takes place in the VBA project => Microsoft Excel Objects => ThisWorkbook

I'm going through there because when the file opens, I want it to compare the date. If today’s date is greater than 25/03/2010 (for example), then I want it to load a specific UserForm that will require a password to use the Excel file; otherwise, I want to close the Excel file!

Anyway, in ThisWorkbook I put this code:
Private Sub Workbook_Open() Application.WindowState = xlMaximized 'I ask to enlarge the window for aesthetics Sheets("Accueil").Select 'I want the tool to open on my home page If Date > "25/03/2011" Then 'Here I set my condition UserForm10.Show 'I launch a UserForm if the condition is met Else: UserForm1.Show 'Otherwise, I launch this UserForm End If End Sub 

If I set Date > "27/01/2011", I will get the first UserForm, and if I set Date > "28/01/2011", I get the second UserForm (because it is the 28th), so I want to say everything is perfect!

But as soon as I put Date > "25/03/2011" (or even 20/02/2011), well, as soon as I get out of January, I feel like it doesn’t work anymore, and it constantly loads the first UserForm...

It's like it doesn’t recognize today’s date...

How can I modify this piece of code that seems correct to me so that it works regardless of the fixed deadline?

A big thank you in advance, hoping that you can help me :)

Thanks to everyone!

Configuration: Windows XP / Safari 534.10

2 answers

pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 772
 
Hello,
I think your problem lies in the type of variable. You are comparing Date, which is of type date, with "......" which would be of type string.
Try this:
Sub test() If CDate(Date) > CDate("25/03/2012") Then MsgBox "Today's date is greater than the indicated date" Else MsgBox "Today's date is less than the indicated date" End If End Sub

That works by changing the date between ""
"Let me guess... Your nickname is 'Bandwidth'? "
- The Court of Flagging Deliquescences - P.Desproges -
0