hello

Gabrysia

New Member
Joined
Dec 28, 2018
Messages
3
I have a problem. How to get the names of sheets from closed workbook using VBA? I can't use Power Query or Microsoft Query... Id like to use VBA.

Thanks in advance.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,
Put this in a normal module.
Code:
Option Explicit
Public Sub DemoGetSheetNames()
    Dim lNumEntries As Long
    Dim szFullName As String
    Dim szFileSpec As String
    Dim aszSheetList() As String
    Sheet1.UsedRange.Clear
    szFileSpec = "Excel Files (*.xl*),*.xl*"
    szFullName = CStr(Application.GetOpenFilename(szFileSpec, , "Select an Excel File"))
    If szFullName <> CStr(False) Then
        GetSheetNames szFullName, aszSheetList()
        lNumEntries = UBound(aszSheetList) - LBound(aszSheetList) + 1
        Sheet1.Range("A1").Resize(lNumEntries).Value = Application.WorksheetFunction.Transpose(aszSheetList())
        Sheet1.Range("A1").EntireColumn.AutoFit
    End If
End Sub
Private Sub GetSheetNames(ByRef szFullName As String, ByRef aszSheetList() As String)
    Dim bIsWorksheet As Boolean
    Dim objConnection As ADODB.Connection
    Dim rsData As ADODB.Recordset
    Dim lIndex As Long
    Dim szConnect As String
    Dim szSheetName As String
    Erase aszSheetList()
    If Application.Version < 12 Then
        szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & szFullName & ";Extended Properties=Excel 8.0;"
    Else
        szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & szFullName & ";Extended Properties=Excel 8.0;"
    End If
    Set objConnection = New ADODB.Connection
    objConnection.Open szConnect
    Set rsData = objConnection.OpenSchema(adSchemaTables)
    Do While Not rsData.EOF
        bIsWorksheet = False
        szSheetName = rsData.Fields("TABLE_NAME").Value
        If Right$(szSheetName, 1) = "$" Then
            szSheetName = Left$(szSheetName, Len(szSheetName) - 1)
            bIsWorksheet = True
        ElseIf Right$(szSheetName, 2) = "$'" Then
            szSheetName = Left$(szSheetName, Len(szSheetName) - 2)
            szSheetName = Right$(szSheetName, Len(szSheetName) - 1)
            bIsWorksheet = True
        End If
        If bIsWorksheet Then
            szSheetName = Replace$(szSheetName, "''", "'")
            ReDim Preserve aszSheetList(0 To lIndex)
            aszSheetList(lIndex) = szSheetName
            lIndex = lIndex + 1
        End If
        rsData.MoveNext
    Loop
    rsData.Close
    Set rsData = Nothing
    objConnection.Close
    Set objConnection = Nothing
End Sub
 
Upvote 0
Hi Gabrysia,

There is a very useful thread here on MrExcel forum:
HTML:
https://www.mrexcel.com/forum/excel-questions/47074-get-worksheet-names-closed-workbook.html

Hope you will find it useful. Take care.
 
Upvote 0
I found another solution of my problem:

Sub names_of_sheets()
Dim sh As Object
n = "C:\Users\USER\Desktop\n.xlsx"
Set sh = GetObject(n).Worksheets


For Each Item In sh
MsgBox Item.Name


Next Item
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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