+ Reply to Thread
Results 1 to 5 of 5

Save file as CSV from XLS after VBA applied

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Save file as CSV from XLS after VBA applied

    Hi,

    I have a set of VBA code that opens my template.xls file and moves specific data over from the file I open which is also an XLS file.

    When I run the below script it saves each file as a CSV which is what I want but it is not a true CSV file as when I open it in notepad the format is that of an excel file.

    The test.xls contains the below VBA and I have also attached the Template.xls

    Here is my current VBA below:
    Sub SaveCSVFiles()
    '
    '
    '
    
        Cells.Select
        Cells.EntireColumn.AutoFit
       
       Dim wb As Workbook
       Dim rng As Range
       Dim sTemplate As String
       Dim sFilename As String
       Dim NextRow As Long
       
       sTemplate = "C:\Documents and Settings\user\My Documents\MPEDIOrders\CFXEDIExport\Temp\Template.xls"
       If Dir(sTemplate) = "" Then
          MsgBox "Template does not exist!"
          Exit Sub
       End If
       
       Set rng = ActiveSheet.Range("B2")
       
       Do Until rng = ""
          On Error Resume Next
          Set wb = Workbooks.Open(sTemplate)
          
          With wb.Worksheets(1)
             .Range("A1").Offset(NextRow).Value = rng.Offset(, -1).Value    ' SHIP_TO_CODE
             .Range("B1").Offset(NextRow).Value = rng.Offset(, 0).Value     ' CUSTOMERPO
             .Range("C1").Offset(NextRow).Value = rng.Offset(, 1).Value     ' NUMBOXES
             .Range("D1").Offset(NextRow).Value = rng.Offset(, 2).Value     ' WEIGHT
             .Range("E1").Offset(NextRow).Value = rng.Offset(, 3).Value     ' USER_DEFINED1 - SCAC CODE
             .Range("F1").Offset(NextRow).Value = rng.Offset(, 4).Value     ' SHIPPING_METHOD
             .Range("G1").Offset(NextRow).Value = rng.Offset(, 5).Value     ' DATE_SHIPPED
             .Range("H1").Offset(NextRow).Value = rng.Offset(, 6).Value     ' SHIP_TO_NAME
             .Range("I1").Offset(NextRow).Value = rng.Offset(, 7).Value     ' SHIP_TO_ADDR1
             .Range("J1").Offset(NextRow).Value = rng.Offset(, 8).Value     ' SHIP_TO_ADDR2
             .Range("K1").Offset(NextRow).Value = rng.Offset(, 9).Value     ' SHIP_TO_CITY
             .Range("L1").Offset(NextRow).Value = rng.Offset(, 10).Value    ' SHIP_TO_STATE
             .Range("M1").Offset(NextRow).Value = rng.Offset(, 11).Value    ' SHIP_TO_ZIP
             .Range("N1").Offset(NextRow).Value = rng.Offset(, 11).Value    ' SHIP_TO_COUNTRY
             .Range("O1").Offset(NextRow).Value = rng.Offset(, 13).Value    ' ORDER_DATE
             .Range("P1").Offset(NextRow).Value = rng.Offset(, 14).Value    ' TRACKINGNO
             .Range("Q1").Offset(NextRow).Value = rng.Offset(, 15).Value    ' PRODUCTID
             .Range("R1").Offset(NextRow).Value = rng.Offset(, 16).Value    ' ENDCUSTOMERPRODUCTID
          End With
          
            Set rng = rng.Offset(1, 0)
          
            'Multi-Item loop
          With wb.Worksheets(1)
            NextRow = 1
            Do Until rng.Offset(-1, 0) <> rng.Value
             .Range("A1").Offset(NextRow).Value = rng.Offset(, -1).Value    ' SHIP_TO_CODE
             .Range("B1").Offset(NextRow).Value = rng.Offset(, 0).Value     ' CUSTOMERPO
             .Range("C1").Offset(NextRow).Value = rng.Offset(, 1).Value     ' NUMBOXES
             .Range("D1").Offset(NextRow).Value = rng.Offset(, 2).Value     ' WEIGHT
             .Range("E1").Offset(NextRow).Value = rng.Offset(, 3).Value     ' USER_DEFINED1 - SCAC CODE
             .Range("F1").Offset(NextRow).Value = rng.Offset(, 4).Value     ' SHIPPING_METHOD
             .Range("G1").Offset(NextRow).Value = rng.Offset(, 5).Value     ' DATE_SHIPPED
             .Range("H1").Offset(NextRow).Value = rng.Offset(, 6).Value     ' SHIP_TO_NAME
             .Range("I1").Offset(NextRow).Value = rng.Offset(, 7).Value     ' SHIP_TO_ADDR1
             .Range("J1").Offset(NextRow).Value = rng.Offset(, 8).Value     ' SHIP_TO_ADDR2
             .Range("K1").Offset(NextRow).Value = rng.Offset(, 9).Value     ' SHIP_TO_CITY
             .Range("L1").Offset(NextRow).Value = rng.Offset(, 10).Value    ' SHIP_TO_STATE
             .Range("M1").Offset(NextRow).Value = rng.Offset(, 11).Value    ' SHIP_TO_ZIP
             .Range("N1").Offset(NextRow).Value = rng.Offset(, 11).Value    ' SHIP_TO_COUNTRY
             .Range("O1").Offset(NextRow).Value = rng.Offset(, 13).Value    ' ORDER_DATE
             .Range("P1").Offset(NextRow).Value = rng.Offset(, 14).Value    ' TRACKINGNO
             .Range("Q1").Offset(NextRow).Value = rng.Offset(, 15).Value    ' PRODUCTID
             .Range("R1").Offset(NextRow).Value = rng.Offset(, 16).Value    ' ENDCUSTOMERPRODUCTID
                  Set rng = rng.Offset(1, 0)
                NextRow = NextRow + 1
            Loop
          End With
                NextRow = 0
          'Save the filename as order number
          cName = Range("A1")
          sFilename = "C:\Documents and Settings\user\My Documents\MPEDIOrders\CFXEDIExport\Ready\" & cName & "-" & rng.Offset(-1).Value & ".csv"
          wb.Close Savechanges:=True, Filename:=sFilename
       
       Loop
          
       'Reset variables
       Set rng = Nothing
       Set wb = Nothing
          
    End Sub
    Thanks for any help,

    David
    Attached Files Attached Files
    Last edited by djfscouse; 07-05-2013 at 12:26 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Save file as CSV from XLS after VBA applied

    to save as csv you have to use saveas, not close Savechanges
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Re: Save file as CSV from XLS after VBA applied

    Hi Patel45,

    Because my VBA saves multiple files which are named by the variable names in the document, it needs to be closed so that it can loop back and run the VBA again.

    It just the formatting that I need to save as.

    Thanks,

    David

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Save file as CSV from XLS after VBA applied

    Hi, David,

    maybe you can save a lot of time when you just open the template once and clear teh contents isnstead of closing the saved file and opening the template again.

    Maybe give this code a try:
    Sub SaveCSVFiles()
    '
    '
    '
    
    
    Dim wb As Workbook
    Dim rng As Range
    Dim sTemplate As String
    Dim sFilename As String
    Dim NextRow As Long
    
    sTemplate = "C:\Documents and Settings\user\My Documents\MPEDIOrders\CFXEDIExport\Temp\Template.xls"
    If Dir(sTemplate) = "" Then
       MsgBox "Template does not exist!"
       Exit Sub
    End If
    
    Application.WindowState = xlMinimized
    Cells.EntireColumn.AutoFit
    
    Set wb = Workbooks.Open(sTemplate)
    Set rng = ThisWorkbook.ActiveSheet.Range("B2")
    
    Do Until rng = ""
       
      'Multi-Item loop
      With wb.Worksheets(1)
        .Range("A1").Offset(NextRow, 0).Resize(1, 18).Value = rng.Offset(, -1).Resize(1, 18).Value    ' SHIP_TO_CODE
        Set rng = rng.Offset(1, 0)
        Do Until rng.Offset(-1, 0) <> rng.Value
          NextRow = NextRow + 1
          .Range("A1").Offset(NextRow, 0).Resize(1, 18).Value = rng.Offset(, -1).Resize(1, 18).Value
          Set rng = rng.Offset(1, 0)
        Loop
      End With
      NextRow = 0
      'Save the filename as order number
      cName = Range("A1")
      sFilename = "C:\Documents and Settings\user\My Documents\MPEDIOrders\CFXEDIExport\Ready\" & cName & "-" & rng.Offset(-1).Value & ".csv"
      wb.SaveAs Filename:=sFilename, FileFormat:=xlCSV
      wb.ActiveSheet.UsedRange.ClearContents
    Loop
    
    wb.Close False
    
    'Reset variables
    Set rng = Nothing
    Set wb = Nothing
    
    Application.WindowState = xlMaximized
       
    End Sub
    Iīm afraid I donīt understand
    It just the formatting that I need to save as.
    No formatting is transferred when you copy values directly. Or do you mean the suffix?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Re: Save file as CSV from XLS after VBA applied

    Hi Holger,

    That works great, thanks for your help.

    David

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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