Compare number to month number?

behedwin

Active Member
Joined
Dec 10, 2014
Messages
398
Why is this code not working?

Code:
Dim dat
Dim yea
Dim mon
Dim da


yea = Left(UserForm4.TextBox_birthyear, 2)
mon = Mid(UserForm4.TextBox_birthyear, 3, 2)
da = Mid(UserForm4.TextBox_birthyear, 5, 2)
dat = DateSerial(yea, mon, da)
If DatePart("m", Date) = mon Then
MsgBox "correct"
Else
MsgBox "incorrect" & mon & " " & DatePart("m", Date)

End If

in the second msgbox i have tested as you can see to print mom and the datepart function.
i get 11 and 11 in the message box.

but i never get the correct messagebox.
both have the value of 11 but the if statement is false still.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
behedwin,

Once you extract mon it is text and your DatePart... is number so try
Rich (BB code):
If DatePart("m", Date) = 1 * mon Then 


Hope that helps.

 
Last edited:
Upvote 0
It is equal to 11.

When running the code i get the incorrect msgbox and it says mon = 11 and so is datepart.

Maybe just add as date in the beginning.
 
Upvote 0

Forum statistics

Threads
1,196,057
Messages
6,013,178
Members
441,752
Latest member
klfezler

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