+ 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
    323

    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

  2. #2
    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

+ 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