change date to text in excel code example

Example 1: excel date to string conversion

'Excel formula to turn a date/time into text:

=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")

'Reference:
'  https://support.office.com/en-us/article/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c

'---------------------------------------------------------------------------------------------

'VBA function to do the same thing:
Function DateToText$(dbl#)
    DateToText = Format(dbl, "DD/MM/YYYY hh:mm:ss AM/PM")
End Function

Example 2: how to change date format in excel

Assuming that you source date that is in the number format dd.mm.yyyy stored as an excel date serial and only formatted to display as dd.mm.yyyy then the best fix is to select the cells you want to modify. Go to your home tab, and select the number format and change it to General. See Green circles in image below. IF the format is already set to general, or when you switch it to general your numbers do not change, then it is most likely that your date in dd.mm.yyyy format is actually text. and will needed to be converted as per OPTION 2 below. However, if the number does change when you set it to general, select the arrow in the bottom right corner of the number area (see red circle).

Tags:

Vb Example