VBA get sheetnames from all excel files in a folder

CaptnAbraham

New Member
Joined
Feb 10, 2022
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Good day dear Excelists...

I have a working code which gets me all the filenames from a given folder (with about 200+ files), looking like this:
-----------------------------------------------------------
Private Sub CommandButton2_Click()

Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$

InitialFoldr$ = "C:\Users\Name\Downloads\Folder\"

With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With

End Sub
-----------------------------------------------------------

The question is... Is there a way to also get the sheet names from within those files? Would that be a new code or is there a way to just attach that parameter here on top?
Normally there should be only one sheet per file but there might be a case where there is two (external files, who knows what silly things other people are up to). Each of the sheet names in these 200+ files are named differently.

Any help or pointers to a solution is much appreciated.

Thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You have to open a workbook to read its sheet names.

Add this before the loop:
VBA Code:
    Dim destCell As Range, wb As Workbook, i As Long
    Set destCell = ActiveCell
and replace this line:
VBA Code:
ActiveCell.Offset(xRow) = xFname$
with these lines:
VBA Code:
    destCell.Offset(xRow) = xFname$
    Set wb = Workbooks.Open(xDirect & xFname, ReadOnly:=True)
    For i = 1 To wb.Sheets.Count
        destCell.Offset(xRow, i) = wb.Sheets(i).Name
    Next
    wb.Close False
The code loops through the Sheets collection, which includes worksheets and chart sheets.
 
Upvote 0
Solution
Found this thread and would like to extend it to search all sub folders. Wondering if someone already has a answer. Other wise I will continue looking.
 
Upvote 0
Found this thread and would like to extend it to search all sub folders. Wondering if someone already has a answer. Other wise I will continue looking.

Search for code which uses FileSystemObject to loop through sub folders and the files in each folder (a recursive procedure) as the basis for modifying the code in this thread. Please start a new thread if you need more help.
 
Upvote 0
Yeap. Found it. Got what I needed. nded up using the recursive version. Now I am paying for the fact that all of these files are in one drive.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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