loop through folders

fraufreda

Board Regular
Joined
Oct 14, 2010
Messages
190
I have set paths to 3 specific files. I cannot figure how to loop through them
Code:
dim path1, path2, path3 as string
dim counter as integer
dim WB as workbook

path1 ="...\a.xls"
path2 ="...\b.xls"
path3 ="...\c.xls"

for counter = 1 to 3
set WB = workbooks.open("Path" & Counter)
counter = counter + 1
next counter

Any help?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
wrong
 
Last edited:
Upvote 0
Try this
Code:
Sub LoopFldr()
    Dim pth(3) As String
    Dim counter As Integer
    Dim WB As Workbook
    
    pth(1) = "C:\Users\Fluff\Documents\Excel files\book1.xls"
    pth(2) = "C:\Users\Fluff\Documents\Excel files\book2.xls"
    pth(3) = "C:\Users\Fluff\Documents\Excel files\book3.xls"

    For counter = 1 To 3
        Set WB = Workbooks.Open(pth(counter))
    Next counter
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
when incorporated into my code it stops in the second workbook and gives an error.
Code:
  Set aWS = Sheets("Master")

For Counter = 1 To 3
    Set tWB = Workbooks.Open(Path(Counter))
    For Each tWS In tWB.Worksheets
     Set uRange = tWS.Range("A1", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
      .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1))
     
     aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
      = uRange.Value 'it gives error here

     RowCount = RowCount + uRange.Rows.Count
    Next
    tWB.Close False

  Sheets("Master").Select
  Application.EnableEvents = True

  Set tWB = Nothing
  Set tWS = Nothing
  Set mWB = Nothing
  Set aWS = Nothing
  Set uRange = Nothing
  
Next Counter
 
Upvote 0
You need to remove the line
Code:
 Set aWS = Nothing
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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