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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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

Forum statistics

Threads
1,215,212
Messages
6,123,655
Members
449,113
Latest member
Hochanz

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