Link to home
Start Free TrialLog in
Avatar of davysouthernboy
davysouthernboy

asked on

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.

ASKER CERTIFIED SOLUTION
Avatar of Tracy
Tracy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DonkeyOte
DonkeyOte

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]
Should add - if you're using a UDF you should just use a standard cell function

=TEXT(datecell,"YYYY-MM-DD")