VBA which opens a workbook in another folder base on a list of names in your current workbook

ExcelNovice_

New Member
Joined
Dec 28, 2021
Messages
1
Platform
  1. Windows
So I have a workbook open . In a control sheet, It has an array of names like

Apple
Orange
Pear

The same workbook but has multiple worksheets and the sheets itself are named apple, orange and pear.

In another folder,i have excel files with the exact same names.

How do i go about writing a macro to loop through the names in the control, to open the file with the same name in another folder and copy the data over?

In sequence :
1. It detects the range of items from wb1.ws1
2. Loop through the first item apple.
3. Opens a worksheet entitled apple in the folder
4. Copy the data
5. Paste in apple worksheet in wb1
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
1. Data source workbooks are saved in C:\DataFolder\
Apple.xlsx, Orange.xlsx, Pear.xlsx
2. Destination workbook has to be saved as a macro-enabled workbook (*.xlsm or *.xlsb)
Make sure you allow the system to run macros:

Here is the destination workbook set-up:
WB_Setup.jpg



And here is the code:

VBA Code:
Option Explicit
Option Compare Text

Sub CopyData()
'=============================================
'   Loop through the list of workbook names
'   and copy data into this workbook
'=============================================
Dim WB As Workbook      'This workbook
Dim LST As Worksheet    'This workbook

Dim xWB As Workbook     'External workbook
Dim xWS As Worksheet    'External workbook

Dim RowNo As Long

Dim FN As String        'Data source filename
Dim FLDR As String      'Folder name
Dim FEXT As String      'Filename extension
Dim FPATH As String     'Full Path

    Application.ScreenUpdating = False
    
    '======================================================
    '   First, identify this workbook and its worksheets
    '======================================================
    Set WB = ActiveWorkbook
    Set LST = WB.Sheets("DataSource")

        '===============================================
        '   Get the:
        '   * data source folder name
        '   * data file name
        '   Make sure the folder name ends with a "\"
        '===============================================
        FLDR = LST.Range("C2")
        FEXT = LST.Range("E2")
        
            '=================================================
            '   Next, loop through the list of data sources
            '=================================================
            RowNo = 2
            
            Do
                '======================================================
                '   Get the data source filename
                '   Trim() = remove any leading- and trailing-spaces
                '======================================================
                FN = Trim(LST.Range("A" & RowNo))
                    '========================
                    '   Form the full path
                    '========================
                    FPATH = FLDR & FN & FEXT
                    Application.StatusBar = "Copying data from: " & FPATH
                    '=================================
                    '   Open the file, if it exist
                    '==================================
                    If Not Dir(FPATH) = "" Then
                        '============================
                        '   Open the data workbook
                        '============================
                        Workbooks.Open FPATH
                            '================================
                            '   Identify the data workbook
                            '================================
                            Set xWB = ActiveWorkbook
                            Set xWS = xWB.Sheets("Sheet1")
                            '=======================================================
                            '   Clean up the destination worksheet before pasting
                            '=======================================================
                            WB.Sheets(FN).Cells.ClearContents
                            '===================
                            '   Copy the data
                            '===================
                            xWS.Range("A1").CurrentRegion.Copy Destination:=WB.Sheets(FN).Range("A1")
                        '=============================
                        '   Close the data workbook
                        '=============================
                        xWB.Close
                        
                    End If
                '======================
                '   Next data source
                '======================
                RowNo = RowNo + 1
                
            Loop Until LST.Range("A" & RowNo) = ""
    
    '========================
    '   Save this workbook
    '========================
    WB.Save
    Application.StatusBar = "Data files have been copied."
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,798
Messages
6,126,974
Members
449,351
Latest member
Sylvine

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