VBA Browse Multiple Files and Copy Data

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I have an open binary excel file (named "Masterfile"). I badly need the codes where the macro will browse from certain Source Folder and import multiple files onto the binary file. There are 3 files inside the Source folder namely: 1.) Survey.xlsx 2.) Checklist.xlsx 3.) CB.csv . I need to copy all 3 files and all the tabs inside it.


Any help will be much appreciated.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Put VBA in standard module in Masterfile
Amend source folder
Code:
Sub ImportFiles()
    Const fpath = "[COLOR=#ff0000]C:\Folder\Source[/COLOR]"
    Dim f, sh, wb, wbMe As Workbook
    Set wbMe = ThisWorkbook
    Application.ScreenUpdating = False
    
    For Each f In Array("Survey.xlsx", "Checklist.xlsx", "CB.csv")
        Set wb = Workbooks.Open(fpath & "\" & f)
            For Each sh In wb.Sheets
                sh.Copy after:=wbMe.Sheets(wbMe.Sheets.Count)
            Next sh
        wb.Close False
    Next f
End Sub
 
Upvote 0
Cool! By the way, can we use GetOpenFilePath instead of the direct path directory as the Source folder directory may change. Lastly, what if I want to import everything in the Source folder not just what I stated initially (as some files might be added but normally in xlsx, xls & csv format)?

Any help will be much appreciated.
 
Upvote 0
Try..

Code:
[COLOR=#2f4f4f]Sub ImportFiles[/COLOR]()
    Dim f, fpath As String
    fpath = GetFolder & "\"
    Application.ScreenUpdating = False
    f = Dir(fpath)
    Do While Len(f) > 0
        Select Case Right(f, Len(f) - InStrRev(f, "."))
            Case "xls", "xlsx", "csv"
                OpenFile (fpath & f)
        End Select
        f = Dir
    Loop
End Sub

[COLOR=#2f4f4f]Function GetFolder[/COLOR]() As String
    Dim f As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = ThisWorkbook.Path
        If .Show <> -1 Then GoTo There
        f = .SelectedItems(1)
    End With
There:
    GetFolder = f
End Function

[COLOR=#2f4f4f]Private Sub OpenFile[/COLOR](filepath)
    Dim sh, wb As Workbook, wbMe As Workbook
    Set wbMe = ThisWorkbook
    Set wb = Workbooks.Open(filepath)
        For Each sh In wb.Sheets
            sh.Copy after:=wbMe.Sheets(wbMe.Sheets.Count)
        Next sh
    wb.Close False
End Sub
 
Upvote 0
It worked! I have just one problem as I put the codes on an add-in macro file so the files we're attached on this file instead of the active workbook. How can attached those files on the active workbook? By the way, the name of the workbook has a word "Master" (like Math Master, English Master, etc) on the file name and usually in .xlsb & .xlxs format. Thanks a lot for the help.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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