Hi,
I want to store today value in a string in VBA Word, e.g. today is Oct 04 2012, the string will be = "04102012". Any simple code can achieve that?
Thanks!
Hi,
I want to store today value in a string in VBA Word, e.g. today is Oct 04 2012, the string will be = "04102012". Any simple code can achieve that?
Thanks!
A quick follow-up that expands on the helpful Format-based suggestion from (and the confirmation from ): using the date functions directly avoids any subtle format-string quirks and keeps the code explicit. If you only need the calendar date (no time component), use Date rather than Now. Keep the value as a string if you need leading zeros preserved.
A safe, locale‑independent way that produces "DDMMYYYY" (for example, "04102012") is to build the parts and zero-pad them:
Dim dateStr As String
dateStr = Right("0" & Day(Date), 2) & Right("0" & Month(Date), 2) & CStr(Year(Date)) If you will use the string in filenames or anywhere chronological sorting matters, prefer an ISO ordering (YYYYMMDD) so filenames sort naturally:
Dim isoDate As String
isoDate = CStr(Year(Date)) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) Notes and troubleshooting:
Format$ returns a true String and can be slightly faster than Format; use it when you rely on Format. m can mean month or minute depending on context—building from Day/Month/Year avoids that ambiguity. These alternatives keep the intent clear and avoid surprises when moving code between machines or locales.
Jump to Post— AndreRet 526You would use something like -
Sub FormatNow() Dim myWorksheetName As String myWorksheetName = Format(Now, "mmmm_yyyy") MsgBox myWorksheetName End Sub
You would use something like -
Sub FormatNow()
Dim myWorksheetName As String
myWorksheetName = Format(Now, "mmmm_yyyy")
MsgBox myWorksheetName
End Sub
Thanks, the code works perfectly well!
Dim dateStr As String
dateStr = Format(Now, "ddmmyyyy")
Only a pleasure. Please mark as solved thanx.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.