+ Reply to Thread
Results 1 to 3 of 3

VBA SaveAs CSV files

Hybrid View

  1. #1
    Registered User
    Join Date
    09-29-2012
    Location
    Swindon
    MS-Off Ver
    Excel 2010
    Posts
    9

    VBA SaveAs CSV files

    Hi

    I have a couple of csv files that I open, copy sepcific data and paste into an excel file. What I then need to do is save the csv files into another folder.

    I have the code below but it just saves both csv files as the same name so when it saves the second one it advises that it already exists???
    Any ideas will be most apreciated

    Formula: copy to clipboard
    Dim i As Integer
    Dim NewFileName As String
    Dim folderPath As String
    Dim filename As String
    Dim wb As Workbook

    folderPath = "C:\Documents and Settings\HartG\My Documents\Projects\Cash errors reports\Imports\"
    strPath = "C:\Documents and Settings\HartG\My Documents\Projects\Cash errors reports\Archive\"
    strFileName = Format(Now(), "yyyy-mm-dd") & "_" & ActiveWorkbook.Name & "CSV"


    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

    filename = Dir(folderPath & "*.CSV")
    Do While filename <> ""
    Application.ScreenUpdating = False
    Set wb = Workbooks.Open(folderPath & filename)
    Call Get_Data
    wb.SaveAs filename:= strPath & strFileName, FileFormat:=xlCSV
    wb.Close SaveChanges:=False
    'Application.DisplayAlerts = False

    filename = Dir
    Loop
    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: VBA SaveAs CSV files

    It looks like the saveas command uses the variable "strFileName" which doesn't change as you loop through the files, maybe but the strFileName line in the do while loop:

    Dim i As Integer
    Dim NewFileName As String
    Dim folderPath As String
    Dim filename As String
    Dim wb As Workbook
      
        folderPath = "C:\Documents and Settings\HartG\My Documents\Projects\Cash errors reports\Imports\"
        strPath = "C:\Documents and Settings\HartG\My Documents\Projects\Cash errors reports\Archive\"
            
    
        If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
        
        filename = Dir(folderPath & "*.CSV")
        Do While filename <> ""
          Application.ScreenUpdating = False
            Set wb = Workbooks.Open(folderPath & filename)
            Call Get_Data
            strFileName = Format(Now(), "yyyy-mm-dd") & "_" & ActiveWorkbook.Name & "CSV"
            wb.SaveAs filename:= strPath & strFileName, FileFormat:=xlCSV
            wb.Close SaveChanges:=False
    'Application.DisplayAlerts = False
    
    filename = Dir
        Loop
      Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-29-2012
    Location
    Swindon
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA SaveAs CSV files

    Brilliant - worked a treat. Thanks very much...

+ 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