+ Reply to Thread
Results 1 to 4 of 4

List Filename with sheet names

Hybrid View

  1. #1
    Registered User
    Join Date
    08-03-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    27

    List Filename with sheet names

    Hello,
    Can someone help me with adding the filename that corresponds to the sheets names? The below code works well, except I need to know which filename each worksheet is tied to. - Thank you

    Sub GetWorkSheetNames()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim OpenWorkbook As Workbook
    Dim wks As Worksheet
    Dim directory As String
    Dim myFile As String
    Dim fileSpec As String
    Dim i As Integer, j As Integer

    ' Set local objects
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(1)

    ' Add path, define file type
    directory = "C:\Users\2017"
    directory = directory & "\"
    fileSpec = ".xls"

    myFile = Dir(directory & "*" & fileSpec)
    i = 1

    Application.ScreenUpdating = False
    Do While myFile <> ""

    ' Open file in hidden mode... might be faster
    Set OpenWorkbook = GetObject(directory & myFile)

    ' Application.Workbooks.Open(Filename:=directory & myFile, ReadOnly:=True)
    ' List sheet names starting at A2
    For j = 1 To OpenWorkbook.Sheets.Count
    ws.Cells(i + 1, 1) = OpenWorkbook.Sheets(j).Name
    i = i + 1
    Next

    OpenWorkbook.Close SaveChanges:=False
    Set OpenWorkbook = Nothing
    myFile = Dir
    Loop
    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: List Filename with sheet names

    Sub GetWorkSheetNames()
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim OpenWorkbook As Workbook
    Dim wks As Worksheet
    Dim directory As String
    Dim myFile As String
    Dim fileSpec As String
    Dim i As Integer, j As Integer
    
    ' Set local objects
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(1)
    
    ' Add path, define file type
    directory = "C:\Users\My\Desktop"
    directory = directory & "\"
    fileSpec = ".xls"
    
    myFile = Dir(directory & "*" & fileSpec)
    i = 1
    
    Application.ScreenUpdating = False
    Do While myFile <> ""
    
    ' Open file in hidden mode... might be faster
    Set OpenWorkbook = GetObject(directory & myFile)
    
    ' Application.Workbooks.Open(Filename:=directory & myFile, ReadOnly:=True)
    ' List sheet names starting at A2
    For j = 1 To OpenWorkbook.Sheets.Count
    ws.Cells(i + 1, 1) = OpenWorkbook.Sheets(j).Name
    ws.Cells(i + 1, 2) = OpenWorkbook.Name   '<--- added this line
    i = i + 1
    Next
    
    OpenWorkbook.Close SaveChanges:=False
    Set OpenWorkbook = Nothing
    myFile = Dir
    Loop
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Registered User
    Join Date
    08-03-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    27

    Re: List Filename with sheet names

    Thanks Logit

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,015

    Re: List Filename with sheet names

    You are welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 01-07-2015, 07:15 PM
  2. [SOLVED] show a list of names on from sheet 1 in sheet 2 when marked active in sheet 1...???
    By stolen_83 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-11-2013, 04:57 PM
  3. Replies: 6
    Last Post: 11-05-2012, 10:04 AM
  4. Getting a list of Sheet Names by excluding certain Sheet Names instantenously..
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-09-2012, 12:16 PM
  5. Replies: 11
    Last Post: 10-06-2011, 02:26 PM
  6. Replies: 1
    Last Post: 10-05-2011, 04:57 PM
  7. Replies: 1
    Last Post: 11-27-2008, 11:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1