Iterate a sequence for every file in a folder

icedMrT

New Member
Joined
Jan 29, 2014
Messages
2
So I have a whole bunch of workbooks that I want to do the same thing to. How can I loop the below bit of code for every file in a given folder?

Code:
    Windows("Inputexample1.xlsm").Activate
    Sheets("Wool").Select
    Range("A8:A23").Select
    Selection.Copy
    Windows("TestMasterSpreadsheet.xlsx").Activate
    Range("A3").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Windows("Inputexample1.xlsm").Activate
    Sheets("Blue").Select
    Range("A7:A9").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("TestMasterSpreadsheet.xlsx").Activate
    Range("AI3").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True

Need a whole lot of things transposed into one workbook (testmasterspreadsheet). from the inputs (inputexample1, inputexample2...)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi icedMrT,

there are various examples available online, but here you go, something like this:

Code:
Sub DoThisForAll()

Set ActWb = ActiveWorkbook

MyPath = Dir("c:\testfolder\")

sFil = Dir(MyPath & "*.xlsx") 'change or add formats
ResRw = 3 'Variable for the row where the results should be pasted

Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped            through
    
    Set SrcWb = Workbooks.Open(sPath & "\" & sFil) 'opens the file
    ' do something
    SrcWb.Worksheets("Wool").Range("A8:A23").Copy
    
    ActWb.Worksheets("Resultworksheet").Range("A" & ResRw).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
    ResRw = ResRw + 1
    
    SrcWb.Close True
    sFil = Dir
Loop

Set ActWb = Nothing

End Sub

Hope this gets you going,

Koen
 
Upvote 0
Code:
Dim wbk As Workbook
Dim Filename As String
Dim Path As String
Path = "C:\Work\testfolder\"
Filename = Dir(Path & "*.xls*")

'--------------------------------------------
'OPEN EXCEL FILES
   Dim no As Integer
   'no is the variable representing row number'
   no = 2

 Do While Len(Filename) > 0  'IF NEXT FILE EXISTS THEN
    Set wbk = Workbooks.Open(Path & Filename)

CODE
CODE
CODE

wbk.Close True
    Filename = Dir
    
    no = no + 1
Loop


Thanks for the reply, did manage to find an example on the net and went with the above one. Looks very similar to the one you posted.
 
Upvote 0

Forum statistics

Threads
1,218,277
Messages
6,141,498
Members
450,365
Latest member
robsandersppc

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