Dynamic Lookup List to copy data onto master sheet

knightofni

New Member
Joined
Sep 22, 2010
Messages
12
Hello all,

I currently have a Macro that opens a workbook, copies data from a sheet and then closes it again, this works fine. However, I now have 43 workbooks that I need to work with and really don't want to have to write out that many file names in the macro.

Therefore I was wondering if anyone could help devise a dynamic loop function that would:
  • Use a list of filenames held in another spreadsheet to open the file
  • Copy Data onto master sheet
  • Close File
  • Repeat through list of filenames
NB: The number of files needed may increase over time which is why I would like the function to be dynamic.

Many thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Why use a list? Put all the files in one folder and try this
Code:
Sub LoopThroughFolder()
    Dim folderPath As String
    Dim filename As String
    Dim WB As Workbook
    
    folderPath = "C:\temp\excel\" 'change to suit your folder path
    
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
    
    filename = Dir(folderPath & "*.xls")
    Do While filename <> ""
        Set WB = Workbooks.Open(folderPath & filename)
        
        'Call a subroutine here to operate on the just-opened workbook
        Call YourMacro(WB)
        
        WB.Close False
        filename = Dir
    Loop
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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