Exporting Selected cells to new CSV keeping formatting

Lewis101

New Member
Joined
Oct 31, 2012
Messages
10
Hi All,

im trying to export cells A2:IU20 in sheet 'StaffTB' to a new CSV file. this works perfectly however Columns F and G need to keep the formatting of yyyy/mm/dd hh:mm:ss, i thought i had it fixed by adding .PasteSpecial xlPasteFormats but this exports it with dd/mm/yyyy hh:mm


Code:
Sub ExportStaffTB()


    Dim myCSVFileName As String
    Dim myWB As Workbook
    Dim tempWB As Workbook
    Dim rngToSave As Range


    Application.DisplayAlerts = False
    On Error GoTo err


    Set myWB = ThisWorkbook
    myCSVFileName = "C:\Delete\StaffTB" & ".csv"


    Set rngToSave = Range("A2:IU20")
    rngToSave.Copy


    Set tempWB = Application.Workbooks.Add(1)
    With tempWB
        .Sheets(1).Range("A1").PasteSpecial xlPasteValues
        .Sheets(1).Range("A1").PasteSpecial xlPasteFormats
        .SaveAs filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
        .Close
    End With
err:
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
As far as I know, csv files cannot save formats.
To maintain the format you want, you'd probably have to change the dates to text first
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,997
Members
449,480
Latest member
yesitisasport

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