Loop through a list of file names

abar_on

New Member
Joined
Aug 21, 2009
Messages
2
I have a procedure that performs certain tasks on multiple Excel files.
At the moment my procedure is set up to select each file name from a
cell, perform the task, move on to next cell for the next file name etc.

Is there any way to create an array of file names and loop through them,
assuming the task performed on each file is identical?

Note that I do not want to loop through all excel file in a subdir. I want
to loop through a selected list of files.

Any help would be greatly appreciated:(
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi

Assuming the list of path & filenames is in cells A1:A10 (amend as appropriate) you can plonk these into an array using:

Code:
Dim vList As Variant
Dim i As Long, wb As Workbook

vList = Range("A1:A10").Value

'now do stuff:

For i = 1 To UBound(vList,1)  'it is a two dimensional array so must specify first dimension in Ubound
 Set wb = Workbooks.Open(vList(i,1))
  'do stuff to wb 
  wb.Close SaveChanges:=True
Next i

Make sense?
 
Upvote 0
Hi

Just try this

Worksheets("sheet1").Visible = True
Worksheets("sheet1").Select
Range("B2").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
For Each sm In Selection
Workbooks.Open (cur_path & "\" & sm.Value & ".xls")

in sheet1 from range B2 you can write the workbook name as many you want & then it will loop only with workbook which you have entered
 
Upvote 0
Fantastic!!! - just ran a quick test and it works.
Do you think this will save a significant amount of processing time (as opposed
to selecting a cell ref. at a time etc.)?
 
Upvote 0
No, not a great deal - most of the time the routine takes will be in opening up the actual files themselves (and then whatever processing you do on them) rather than how you open them.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

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