VBA: Date serial number

dave2018

Board Regular
Joined
Mar 20, 2018
Messages
223
no matter what i try, i cant get vba to return the serial number of the date.

i want for example, to input this: 6/18/2018 and return this: 43269

in a worksheet, u change the format to general, but how to do this in vba?

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Code:
Sub test()
Dim dte As Date
dte = #6/18/2018#
MsgBox CLng(dte)
End Sub
Dear Joemo, CLng(dte) has solved my frustrating date calculation issues I had in my macro.
Thanks a lot.
 
Upvote 0
Can you clarify on the usage of # before and after the date.
The hash marks (#) indicate to VBA that what the marks enclose is a date (a number not text).
VBA Code:
Sub test()
MsgBox CLng(#6/18/2021#)
'next line generates a run time error for type mismatch
MsgBox CLng("6/18/2021")
End Sub
 
Upvote 0
The hash marks (#) indicate to VBA that what the marks enclose is a date (a number not text).
VBA Code:
Sub test()
MsgBox CLng(#6/18/2021#)
'next line generates a run time error for type mismatch
MsgBox CLng("6/18/2021")
End Sub
Thank you for the explanation.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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