Excel VBA Function To Convert Any Given Date to MySQL YYYY-MM-DD format?

Hi.  I need a fuction that will take any given date value and convert that date value into MySQL's "YYYY-MM-DD" format.

I originally tried something like:

Range(CellRange).Select
Selection.NumberFormat = "yyyy-mm-dd"
NoteDate = Worksheet.Range(CellRange).Value

But apparently, that just changes its appearance, not its value.

davysouthernboyAsked:
Who is Participating?
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.

TracyVBA DeveloperCommented:
Try this:
NoteDate  = Format(Range(CellRange).Value, "YYYY-MM-DD")
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
DonkeyOteCommented:
In XL the value of any date value however it may be formatted is the same - an Integer.

If you want as a literal text string you will need to adjust accordingly - ie format cell as Text and then apply your Format, eg:

[code]
With Range(cellRange)
    .NumberFormat = "@"
    .Value = Format(.Value,"YYYY-MM-DD")
End With
[/code]
0
DonkeyOteCommented:
Should add - if you're using a UDF you should just use a standard cell function

=TEXT(datecell,"YYYY-MM-DD")
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
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.