Set date format in workbook created by VBA

GonzoB

New Member
Joined
Dec 4, 2021
Messages
43
Hi Helpers,

I have the following code which creates a new workbook on my computer from a worksheet:

VBA Code:
Sub Create_New_File()

    Dim ImportSheet As String
    ImportSheet = ThisWorkbook.Name
  
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    objects = Array("new_file")
    
    For Each Object In objects
        fn = ThisWorkbook.Path & Application.PathSeparator & Object
        On Error Resume Next
        ThisWorkbook.Sheets(Object).Copy
        If ImportSheet <> ActiveWorkbook.Name Then
            ActiveWorkbook.SaveAs Filename:=fn, FileFormat:=xlCSVUTF8, CreateBackup:=False
            ActiveWorkbook.Close
        End If
    Next Object
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub


On the source sheet (new_file) there's a date field which contains dates in this format: yyyy-mm-dd. My problem is that in the newly created workbook the date format becomes mm/dd/yyyy. Do you know what modification to do in the VBA to have yyyy-mm-dd in the new workbook?

Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You are saving the new workbook as a csv file.

VBA Code:
  ActiveWorkbook.SaveAs Filename:=fn, FileFormat:=xlCSVUTF8, CreateBackup:=False

There is no way to save formatting to a csv file, so when you use excel to open the new CSV file, it will apply the default date format to any cell it interprets as a date. Your choices are are to save the file as an xlsx workbook, change your default date settings, or live with mm/dd/yyyy.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top