In one of my projects I had to read all worksheet names from several Excel files.
Here is the code for getting all worksheet names from a closed or already opened Excel file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
' @Author - Alexander Bolte' @Change Date - 26.12.2013' @Description - Creates a list of all sheet names in workbook available under given file path.' @Param filePath - Path to Microsoft Excel file the sheet name collection should be created from.' @Returns - VBA.Collection holding all sheet names from given Microsoft Excel file.PublicFunctiongetSheetNames(ByValfilePathAsString)AsVBA.CollectionDimwrkAsWorkbookDimsheetNamesAsNewVBA.CollectionOnErrorGoToerrHandle:Setwrk=getOpenedWorkbook(filePath)IfwrkIsNothingThenSetwrk=Application.Workbooks.Open(filePath)EndIf' Get the sheet names for one file.SetsheetNames=getSheetNamesFromWorkbook(wrk)' Close workbook without saving.wrk.CloseFalseerrHandle:IfErr.Number<>0ThenErr.ClearEndIfSetwrk=NothingSetgetSheetNames=sheetNamesEndFunction