0

I have this code to show the date in the following format like (Date: March 22, 2015) but it shows it as (Date: 3/22/2015) so please assist.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim dtmTest As Date
dtmTest = DateValue(Now)
Range("A1") = Format(dtmTest, "mmmm dd, yyyy")
Range("A1") = "Date : " & dtmTest
End Sub

2 Answers 2

1

You need to change the cell formatting to text to obtain the desired effect.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Sets cell formatting to string
Range("A4").NumberFormat = "@"
'fills range with formatted date
Range("A4").Value = Format(Now, "mmm, dd yyyy")
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

At which point, it is easier to assign the date format to the cell in the first place and then just set it to Now. At which point you don't need a macro, only the format on the cell and the formula, =NOW().
Many thanks Guys, it works as I wished. really appropriate it.
@NabilAmer, Gserg's comment says a method that uses no code. I think it's pretty good if you don't want to use VBA.
0

The solution posted works, but your original code was almost correct. You reformatted the date with the format function, but then overwrote it with the dtmtest variable. Try this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim dtmTest As Date
dtmTest = DateValue(Now)
Range("A1") = "Date : " & Format(dtmTest, "mmmm dd, yyyy")
End Sub

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.