excel vba activateworkbook.save changes date format

02773401 0 Reputation points
2025-03-13T07:42:16.75+00:00

I'm running a macro that creates a blank workbook using Workbook.Add and saves it in a specified location as a CSV (FileFormat:=xlCSV). It then copies data from another workbook into this sheet with formatting (Paste:=xlPasteValuesAndNumberFormats). Then I run "ActiveWorkbook.Close SaveChanges:=True".

When I open the CSV file, using Notepad to see the true format, the date formats are incorrect.

The strange thing is that, if I comment out "ActiveWorkbook.Close SaveChanges:=True" and manually close and save the file, using my mouse. the formatting is correct.

If I run "ActiveWorkbook.Save" and manually close the file, the format is also incorrect.

I can only deduce that it is vba ".save" that is changing the date formats.

I have checked my machine's regional settings and these are correct...

Does anyone know of a way to deal/fix this?

TIA!

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,348 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alistair Blades 0 Reputation points
    2025-05-01T16:45:49.0733333+00:00

    There is an issue where excel needs to notice formats when you save to CSV through VBA.

    We can get around it multiple ways. We could write the file more complicated ways, but one which you will likely prefer is seeing the workbook copy over.

    I've taken the liberty of making a new macro for you which follows some better practice. "Activeworkbook" could pickup the wrong file for example. I hope this helps.

    
    Public Sub CreateNewCsv()
    
        Const FileNamePrefix As String = "CSV Export "
        Const PickupSheet As String = "InformationSheet"
        Const PickupRange As String = "A1:B2"
        Const DateFormatWanted As String = "dd-mm-yyyy"
    
        Dim FilePath As String
        Dim FileName As String
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim arr As Variant
        Dim iRow As Long
        
        On Error GoTo ErrHandler
        
        ' Get Filepath to save new document
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select folder to save CSV"
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count = 0 Then GoTo Ending Else FilePath = .SelectedItems.Item(1)
        End With
        
        ' Create a filename
        FileName = FileNamePrefix & Format(Now(), "YYYYMMDDHHNNSS")
        
        ' Create new workbook and get the first sheet
        Set wb = Workbooks.Add
        Set ws = wb.Worksheets(1)
        
        ' Grab our date data
        arr = ThisWorkbook.Sheets(PickupSheet).Range(PickupRange).Value2
        
        ' Convert the dates to string
        '   This will only change the first column in this example
        For iRow = 1 To UBound(arr, 1)
            arr(iRow, 1) = "'" & Format(arr(iRow, 1), DateFormatWanted)
        Next iRow
        
        ' Input the values to our new workbook's worksheet
        ws.Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
            
        ' Save the workbook as a CSV
        wb.SaveAs FilePath & Application.PathSeparator & FileName, xlCSV
        wb.Close False
    
        GoTo Ending
    ErrHandler:
        MsgBox "Unexpeced error during 'CreateNewCsv'", vbExclamation
    Ending:
    End Sub
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.