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