From where? A specific directory? Is that indicated somewhere?Need to type name of sheet and open file/s who have that sheet.
Dan, I would like to see how it could be done on closed workbooks, for my own knowledge, if you don't mind!It is possible to access the sheet names of a closed workbook, but you need to use VBA (ADODB) in order to do it. Would you be open to a VBA based solution?
@Joe4 - Certainly! ADO isn't my strong suit, but I have long used the following to extract the sheet names from workbooks:Dan, I would like to see how it could be done on closed workbooks, for my own knowledge, if you don't mind!
Function GetSheetNames(ByVal FilePath As String) As String
Dim Conn As Object, RecSet As Object
Set Conn = CreateObject("ADODB.Connection")
Set RecSet = CreateObject("ADODB.Recordset")
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & ";Extended Properties=Excel 8.0;"
Set RecSet = Conn.OpenSchema(20) ' 20 = adSchemaTables
While Not RecSet.EOF
GetSheetNames = GetSheetNames & IIf(Trim(GetSheetNames) = "", "", ",") & RecSet("TABLE_NAME")
RecSet.MoveNext
Wend
RecSet.Close
Conn.Close
End Function
Sub Test()
Dim Filename As String
' Assume a simple workbook with two worksheets - Sheet1, Sheet2
Filename = "D:\test.xlsx"
Debug.Print GetSheetNames(Filename)
' Output into the immediate window: Sheet1$,Sheet2$
End Sub
Sheet1$,Sheet2$
. From this point it is a simple string manipulation to remove the dollar signs, convert it to an array, etc etc., as required. I should stress that I'm not the author of the routine above (I don't know who is) - I've simply had it sitting in my code snippet directory for years!Cool!@Joe4 - Certainly! ADO isn't my strong suit, but I have long used the following to extract the sheet names from workbooks:
VBA Code:Function GetSheetNames(ByVal FilePath As String) As String Dim Conn As Object, RecSet As Object Set Conn = CreateObject("ADODB.Connection") Set RecSet = CreateObject("ADODB.Recordset") Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & ";Extended Properties=Excel 8.0;" Set RecSet = Conn.OpenSchema(20) ' 20 = adSchemaTables While Not RecSet.EOF GetSheetNames = GetSheetNames & IIf(Trim(GetSheetNames) = "", "", ",") & RecSet("TABLE_NAME") RecSet.MoveNext Wend RecSet.Close Conn.Close End Function
And you can run it as follows:
VBA Code:Sub Test() Dim Filename As String ' Assume a simple workbook with two worksheets - Sheet1, Sheet2 Filename = "D:\test.xlsx" Debug.Print GetSheetNames(Filename) ' Output into the immediate window: Sheet1$,Sheet2$ End Sub
Assuming a workbook comprising, say, two worksheets - Sheet1 and Sheet2 - the output will beSheet1$,Sheet2$
. From this point it is a simple string manipulation to remove the dollar signs, convert it to an array, etc etc., as required. I should stress that I'm not the author of the routine above (I don't know who is) - I've simply had it sitting in my code snippet directory for years!
Yes, I think that's right! Unless I'm missing something, it should be pretty straight forward.Cool!
So if they have a whole directory of files to work though, they will just need to loop through them and feed each file name to the code, and write the results back.
Function BrowseForFolder(Optional ByVal Prompt As String = "Please select folder") As String
BrowseForFolder = CreateObject("Shell.Application").BrowseForFolder(0, Prompt, 1, "").self.Path & "\"
End Function
TargetPath = BrowseForFolder()