1

The VBA built-in Date variable can give me today's date (8/25/21 as the time this post is written). However, I really want the date in the mm/dd/yyyy format for future projects and store it in a variable - myDate. I just couldn't think of an easy string manipulation solution to get the desired result.

I've tried put the Date variable in excel sheets and then change the number format using the below code:

[A1]=Date
[A1].NumberFormat="mm/dd/yyyy"
myDate=[A1].value
debug.print myDate

Even though the number format code will change the appearance of cell [A1], making it look like it's in the desired format mm/dd/yyyy, the debug.print line still gives 8/25/21 instead of 08/25/2021

3
  • 2
    Number format doesn't change the underlying value. myDate = Format$(Date, "mm/dd/yyyy") Commented Aug 25, 2021 at 20:19
  • you are right. Can you please explain what does the "$" sign do? Commented Aug 28, 2021 at 18:10
  • 1
    Format$ returns a String. Format returns a Variant/String. Either works here Commented Aug 28, 2021 at 18:21

1 Answer 1

1

A date value holds no format. Apply the format for display - that includes when calling Debug.Print:

Dim myValue As Date

[A1] = Date
[A1].NumberFormat = "mm/dd/yyyy"
myDate = [A1].Value
Debug.Print Format(myDate, "mm/dd/yyyy")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.