vbanoob1234
New Member
- Joined
- Aug 8, 2016
- Messages
- 26
Hello everyone,
I currently have a macro that opens all excel files in a folder. However, I want to take this macro one step further, and open all excel files in the folder, and sub folder.
Subfolder names are not consistent.
Please see my macro below:
Current Macro Explanation:
- open all xls. in a source folder of my choice (want it to do this for sub-folders)
- creates a worksheet called "Master" at the end
- list all worksheet names in the "Master" worksheet
- save change, closes workbook
- next workbook
Any help is appreciated. Thanks
Dim wb As Workbook, ws As Worksheet
Dim sFil As String, sPath As String
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Application.ScreenUpdating = True
'OPENS THE PATH OF FOLDER - want to make it open all excel in sub folders as well
sPath = "G:\<acronym title="visual basic for applications" style="border-******* 0px 0px **** border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym>\ARFilesTesting" 'location of files, don't forget the "" at the end
sFil = Dir(sPath & "*.xls") 'change or add formats
Application.DisplayAlerts = False
'FIND ALL THE FILES
Do Until sFil = ""
Workbooks.Open sPath & sFil
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
'CREATE A WORKSHEET AT THE END CALLED "MASTER"
Set wsht = ActiveWorkbook.Sheets.Add(After:= _
ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
wsht.Name = "Master"
'LIST ALL WORKSHEETS NAMES
Dim x As Integer
For x = 1 To Worksheets.Count
Cells(x, 1).Value = Worksheets(x).Name
Next x
ActiveWorkbook.Close savechanges:=True
Next
sFil = Dir()
Loop
Application.DisplayAlerts = True
End Sub
I currently have a macro that opens all excel files in a folder. However, I want to take this macro one step further, and open all excel files in the folder, and sub folder.
Subfolder names are not consistent.
Please see my macro below:
Current Macro Explanation:
- open all xls. in a source folder of my choice (want it to do this for sub-folders)
- creates a worksheet called "Master" at the end
- list all worksheet names in the "Master" worksheet
- save change, closes workbook
- next workbook
Any help is appreciated. Thanks
Dim wb As Workbook, ws As Worksheet
Dim sFil As String, sPath As String
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Application.ScreenUpdating = True
'OPENS THE PATH OF FOLDER - want to make it open all excel in sub folders as well
sPath = "G:\<acronym title="visual basic for applications" style="border-******* 0px 0px **** border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym>\ARFilesTesting" 'location of files, don't forget the "" at the end
sFil = Dir(sPath & "*.xls") 'change or add formats
Application.DisplayAlerts = False
'FIND ALL THE FILES
Do Until sFil = ""
Workbooks.Open sPath & sFil
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
'CREATE A WORKSHEET AT THE END CALLED "MASTER"
Set wsht = ActiveWorkbook.Sheets.Add(After:= _
ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
wsht.Name = "Master"
'LIST ALL WORKSHEETS NAMES
Dim x As Integer
For x = 1 To Worksheets.Count
Cells(x, 1).Value = Worksheets(x).Name
Next x
ActiveWorkbook.Close savechanges:=True
Next
sFil = Dir()
Loop
Application.DisplayAlerts = True
End Sub