VBA Incorrect Date Format When Saving As .csv

ir121973

Active Member
Joined
Feb 9, 2008
Messages
371
Hi, I wonder whether someone may be able to help me please.

I'm using the code below to copy a selected range of cells, create a new workbook and then paste the data into sheet 1 from the source to the recipient sheet.

Code:
Sub copysheet()
    Application.ScreenUpdating = False


    Dim Src As Worksheet, r As Range
    Dim LastRow As Long
    Dim MyString As String
    Dim wkb As Workbook


    MyString = "SC, DV"
    ary = Split(MyString, ",")


    Set Src = ThisWorkbook.Sheets("Amalgamation of Search")
    LastRow = Src.Cells(Cells.Rows.Count, "C").End(xlUp).Row
    Set r = Src.Range("C2:C" & LastRow)


    With r
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=(ary), Operator:=xlFilterValues
    End With


    Src.Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).Copy
    Set wkb = Workbooks.Add
    Sheets("Sheet1").Range("A1").PasteSpecial xlValues
    Src.Range("C1:C" & LastRow).SpecialCells(xlCellTypeVisible).Copy
    wkb.Sheets("Sheet1").Range("B1").PasteSpecial xlValues
    Src.Range("D1:D" & LastRow).SpecialCells(xlCellTypeVisible).Copy
    wkb.Sheets("Sheet1").Range("C1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    Src.AutoFilterMode = False
    ActiveWorkbook.SaveAs Filename:= _
        "\\c\s\CAF1\Digital Delivery Group\DDCOPS\Data Security\SC Data.csv", _
    FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbook.Close
    Workbooks.Open Filename:="""\\c\s\CAF1\Digital Delivery Group\DDCOPS\Data Security\SC Data.csv"
    ActiveSheet.Range("C2:C" & LastRow).Select
    Selection.Replace what:=",", replacement:="", Lookat:=xlPart
    Application.ScreenUpdating = True
End Sub

The macro copies and pastes the data without any issue except for a date column (Column C) in the recipient sheet.

The source sheet date column is dd/mm/yyyy, the format when the data is copied to the recipient sheet becomes dd-mm-yyyy. However, I need this to be in the format "dd mmm yyyy" because I'm importing this into a third party application which doesn't not accept forward slashes.

Over the last few days I've tried so many ways to try and get this to work e.g. using the format (ddd, mmm, yyyy) then removing the commas. I've then tried paste this as text, but I can't get this to work. I've then used the 'pastevalues' method which again doesn't provide the format I need.

I just wondered whether someone could take a look at this please and let me know where I've gone wrong.

Many thanks and kind regards

Chris
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi @yky, thank you for coming back to me with this.

Could you perhaps explain how I would incorporate this into my macro please?

Many thanks and kind regards

Chris
 
Upvote 0
Hi yky, thank you for coming back to me with this.

I've managed to sort this by using the format "mmmm dd yyyy".

Thank you for all your help and regards

Chris
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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