Loop folder, copy csv and assign new filename

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
Hi All Member,
I have 2 csv files in download folder and I would like to copy each file twice to current workbook and rename them accordingly.
File 1: Western => Rename to Western_Q1 and Western_Q2
File 2: Eastern => Rename to Eastern_Q1 and Eastern_Q2

I tried the following code, but it tends to copy 4 times for File 1

VBA Code:
Sub CopyFiles()

    Dim wb As Workbook
    Dim FSO As Object, Folder As Object, file As Object
    Dim Region As Variant, Quarter As Variant
   
    Set wbMstr = ThisWorkbook
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder("C:\Users\My\Downloads\")
   
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
   
   
    For Each file In Folder.Files
    
        Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
             
        For Each Region In Array("Western", "Eastern")
              For Each Quarter In Array("Q1", "Q2")
            
             wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
             ActiveSheet.Name = Region & "_" & Quarter
            Next Quarter 
        Next Region
        
        wb.Close False
    Next

End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about
VBA Code:
    For Each file In Folder.Files
    
        Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
             
              For Each Quarter In Array("Q1", "Q2")
            
             wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
             ActiveSheet.Name = wb.Sheets(1).Name & "_" & Quarter
            Next Quarter
        
        wb.Close False
    Next
 
Upvote 0
How about
VBA Code:
    For Each file In Folder.Files
   
        Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
            
              For Each Quarter In Array("Q1", "Q2")
           
             wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
             ActiveSheet.Name = wb.Sheets(1).Name & "_" & Quarter
            Next Quarter
       
        wb.Close False
    Next
Hi Fluff thanks for your valuable input. The csv file provided different source did not match my expectation. How should I assign first file (wb.Sheets(1).Name) to Western and second file to Eastern ?
 
Upvote 0
Are the two files called Western & Eastern?
 
Upvote 0
Are the two files called Western & Eastern?
The filename for these 2 csv files are dynamic due to different senders. Eg:
Sender 1: Western_File, Eastern_File
Sender 2: Data_1, Data_2
But I would like to rename first csv as Western and 2nd csv as Eastern. Is this something can be achieved using VBA ?
 
Upvote 0
How would you know which is which?
 
Upvote 0
Ok, how about
Excel Formula:
Sub CopyFiles()

    Dim wb As Workbook
    Dim FSO As Object, Folder As Object, file As Object
    Dim Region As Variant, Quarter As Variant
    Dim i As Long
    
    Set wbMstr = ThisWorkbook
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder("C:\Users\My\Downloads\")
   
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
   
   
   For Each file In Folder.Files
      If i = 0 Then Region = "Western" Else Region = "Eastern"
      i = i + 1
      Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
      
      For Each Quarter In Array("Q1", "Q2")
      
         wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
         ActiveSheet.Name = Region & "_" & Quarter
      Next Quarter
      
      wb.Close False
   Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,712
Messages
6,174,031
Members
452,542
Latest member
Bricklin

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