+ Reply to Thread
Results 1 to 7 of 7

Save Range as CSV and update range format in CSV before saving.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-21-2018
    Location
    Northampton, England
    MS-Off Ver
    2010
    Posts
    61

    Save Range as CSV and update range format in CSV before saving.

    Hello all,

    I have the below code however i need to edit the CSV data before it saves as currently the copy and past section looses the formating.

    Sub save_to_CSV()
    
        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 = myWB.Path & "\" & "Staffingfile-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
    
        Set rngToSave = Range("B2:F300")
        rngToSave.Copy
    
        Set tempWB = Application.Workbooks.Add(1)
        With tempWB
            .Sheets(1).Range("A1").PasteSpecial xlPasteValues
            .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
            .Close
        End With
         
    err:
        Application.DisplayAlerts = True
        
    End Sub

    This is the section that im looking to do in the CSV the following
        Columns("D:D").Select
        Selection.NumberFormat = "yyyy-mm-dd"
    '
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "CountryCode"
        Range("B1").Select
        ActiveCell.FormulaR1C1 = "StationCode"
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "TrainingName"
        Range("D1").Select
        ActiveCell.FormulaR1C1 = "Date"
        Range("E1").Select
        ActiveCell.FormulaR1C1 = "Capacity"
    Thanks for your help.

  2. #2
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Save Range as CSV and update range format in CSV before saving.

    A sample workbook would be much clear with what you want to achieve. see the yellow banner

    You are pasting as values with no formatting and losing formatting. Are you trying to repeat the formats from the original document? If so why not paste the formats as well?

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Save Range as CSV and update range format in CSV before saving.

    If the original data is formatted properly then just try remove xlPasteValues.

  4. #4
    Registered User
    Join Date
    01-21-2018
    Location
    Northampton, England
    MS-Off Ver
    2010
    Posts
    61

    Re: Save Range as CSV and update range format in CSV before saving.

    Thanks Jindon, The data in the origional document it correct but is prone to other users changing headings or format. What im attempting to do is merge the first and second set of code into one macro so the process would be,
    Copy and Past range from core document to new document > Edit Formating and headings > Save under the new file name.

    Thanks in advance.
    Last edited by AliGW; 09-24-2021 at 04:28 AM. Reason: PLEASE don't quote unnecessarily!

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Save Range as CSV and update range format in CSV before saving.

    If you want to combine 2 codes then should be look like(untested)
    Sub save_to_CSV()
    
        Dim myCSVFileName As String
        Dim myWB As Workbook
        Dim tempWB As Workbook
        Dim rngToSave As Range
    
        Application.DisplayAlerts = False
    
        Set myWB = ThisWorkbook
        myCSVFileName = myWB.Path & "\" & "Staffingfile-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
    
        Set rngToSave = Range("B2:F300")
        rngToSave.Copy
    
        Set tempWB = Application.Workbooks.Add(1)
        With tempWB
            With .Sheets(1)
                .Range("A1").PasteSpecial
                .Range("a1:e1") = Array("CountryCode", "StationCode", "TrainingName", "Date", "Capacity")
                .Columns("d").NumberFormat = "yyyy-mm-dd"
            End With
            .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
            .Close
        End With
        Application.DisplayAlerts = True
        
    End Sub

  6. #6
    Registered User
    Join Date
    01-21-2018
    Location
    Northampton, England
    MS-Off Ver
    2010
    Posts
    61

    Re: Save Range as CSV and update range format in CSV before saving.

    Thanks jindon
    All seems to work however the date format still comes up as UK Date "dd/mm/yyyy" cont figure out why it doesnt conform to the format stated
    Last edited by AliGW; 09-24-2021 at 04:46 AM. Reason: PLEASE stop quoting unnecessarily!

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Save Range as CSV and update range format in CSV before saving.

    That means that column is Text date, not Serial date, I guess.

    Or just realized that "Date" Column is "E" in new workbook...
    Last edited by jindon; 09-24-2021 at 04:49 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Save and restore (or transfer) a range's format properties
    By Warbe in forum Tips and Tutorials
    Replies: 0
    Last Post: 07-24-2019, 05:59 PM
  2. [SOLVED] Saving a range of excel cells as a script file (scr.) without "Save As" prompt screen
    By blake.dennis in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-03-2016, 01:16 PM
  3. Replies: 0
    Last Post: 02-16-2013, 01:39 PM
  4. [SOLVED] Saving a specific cell range in a pdf format
    By jjin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-02-2013, 10:27 AM
  5. [SOLVED] Saving range of cells in a PDF format using a button on excel worksheet
    By jjin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-13-2013, 12:18 PM
  6. [SOLVED] Save selected range in a pdf format & email the file to a someone automatically?
    By jjin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-13-2013, 12:16 PM
  7. Save a range to binary file (including its format)
    By Cool Sport in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-07-2005, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1