Date and Time Format for range in Excel VBA

Hi all.

I have the following code that formats some of the columns in my Excel worksheet:

Dim rng2 As Long

With Worksheets("Sheet1")

    rng2 = .Range("A" & Rows.Count).End(xlUp).Row
.Range("N2:N" & rng2).Style = "Currency"

.Range("A1:R" & rng2).Columns.AutoFit
            
End With

Open in new window


Now I want to format column F to show the date and time in the following format: mm/dd/yyyy hh:mm AM/PM

But I'm not able to do so, here's what I've tried so far:

.Range("F2:F" & rng2).NumberFormat = "mm/dd/yyyy hh:mm AM/PM"
.Range("F2:F" & rng2) = Format("mm/dd/yyyy hh:mm AM/PM")

Any ideas?

Thank you in advance.
printmediaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The first one should work. What is the result? You can always try in the VBA Editor's Direct Window, e.g. with [A1].NumberFormat, for testing.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Saqib Husain, SyedEngineerCommented:
What happens with the first option?
0
printmediaAuthor Commented:
You're right it does work, the problem was the column was not wide enough to see the time portion it was just displaying the date. Thanks!
0
Anthony BerenguelCommented:
in testing
.Range("F2:F" & rng2).NumberFormat = "mm/dd/yyyy hh:mm AM/PM"

Open in new window

worked for me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.