Tweak my macro to include sub folders on loop

StevenAncel

New Member
Joined
Dec 9, 2015
Messages
38
I feel like my request should be simple.

Here is what my macro does:
1. Copies range from 'Job Type vLookup' tab
2. Opens files from folder listed in A2
3. Adds new tab to opened workbook
4. Paste the information
5. Rename new tab: Job Type vLookup'
6. Save and Close Workbook
7. Loop for each file in folder listed in A2

I need it to dig and run on each excel file found.

For instance:

Master folder > subfolder1 > subfolder2 > excel file <-- Macro runs on that excel file

-------------------------------> subfolder3 > excel file <-- Macro runs on that excel file

-------------------------------> subfolder4 > excel file <-- Macro runs on that excel file




Code:
Sub Copy_JobTypevLookup()Dim MyFile As String
Dim sh As Worksheet
Set sh = ThisWorkbook.ActiveSheet
MyPath = "\\ac.local\AC-DFS\UserData\steven.lawson\Desktop\" & Range("A2").Text
MyFile = Dir(MyPath & "\*.xlsx")


    Sheets("Job Type vLookup").Select
    Range("A1:B1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Sheet1").Activate
    Range("A1").Activate
Do While MyFile <> ""
Workbooks.Open Filename:=MyPath & "\" & MyFile
With Sheets(1)
    Sheets("Sheet1").Activate
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Columns("A:B").EntireColumn.AutoFit
    ActiveSheet.Name = "Job Type vLookup"
    Range("A1").Select
End With
    Range("A1").Select
ActiveWorkbook.Close True
MyFile = Dir
Loop
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

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