Add Each CSV To Current Workbook

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
Hi Member,

I have 2 csv files in download folder. I would like to add each csv file twice to current workbook and rename them accordingly. How should I amend the below coding ?

VBA Code:
Sub Add_CSV()

Dim wb As Workbook
Dim FSO As Object, Folder As Object, file As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\Users\Username\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 sheet In wb.Sheets
        sheet.Copy After:=ThisWorkbook.Sheets(1)
    Next
    wb.Close False
Next

Sheets(2).Name = "File1_A"
Sheets(3).Name = "File1_B"
Sheets(4).Name = "File2_A"
Sheets(5).Name = "File2_B"

EndSub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
See if this does what you need:

VBA Code:
Sub Add_CSV()

    Dim wb As Workbook, wbMstr As Workbook
    Dim FSO As Object, Folder As Object, file As Object
    Dim fileNo As Long
    Dim shtSuffix As Variant
   
    Set wbMstr = ThisWorkbook
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder("C:\Users\Username\Downloads\")
   
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
   
    fileNo = 0
   
    For Each file In Folder.Files
        fileNo = fileNo + 1
        Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
             
        For Each shtSuffix In Array("A", "B")
            wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            ActiveSheet.Name = "File" & fileNo & "_" & shtSuffix
        Next shtSuffix
        wb.Close False
    Next

End Sub
 
Upvote 0
Hi Alex,

Thanks for your input. How should I replace "File" & fileNo if the filename is dynamic ?
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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