ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Querying sheet of unknown name (https://www.excelbanter.com/excel-programming/296467-querying-sheet-unknown-name.html)

ChaunceyMo

Querying sheet of unknown name
 
Hi all,

I am importing excels into code via an adodb connection, but have ru
into the problem of many of the excels not having standard sheet name
(ie- "import" instead of "Sheet1"). Is there a way to do a SELEC
query without knowing the name of the sheet? This is furthe
complicated by the workbooks potentially having two sheets rather tha
just one, again with an unknown name.

A method that iterates through the sheets sequentially would b
terrific.

Thanks in advance,
M

--
Message posted from http://www.ExcelForum.com


Bob Phillips[_6_]

Querying sheet of unknown name
 
Mo,
assuming that the sheet is always the first sheet, you could get the name
from the file before you run the query. Here is a sample function to do that

Function SheetsADOX(FileName As String) As Variant
Dim oConn As Object
Dim oCat As Object
Dim oTable As Object
Dim sConnString As String
Dim sFileName As String
Dim sTableName As String
Dim aSheets As Variant
Dim cLength As Integer
Dim iTestPos As Integer
Dim iStartpos As Integer

sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & FileName & ";" & _
"Extended Properties=Excel 8.0;"

Set oConn = CreateObject("ADODB.Connection")
On Error Resume Next
oConn.Open sConnString
If Err.Number < 0 Then
SheetsADOX = ""
Exit Function
Else
On Error GoTo 0
Set oCat = CreateObject("ADOX.Catalog")
Set oCat.ActiveConnection = oConn

ReDim aSheets(oCat.Tables.Count - 1)
sTableName = oCat.Tables(1).Name
cLength = Len(sTableName)
iTestPos = 0: iStartpos = 1
'Worksheet name with embedded spaces are enclosed by single quotes
If Left(sTableName, 1) = "'" And Right(sTableName, 1) = "'" Then
iTestPos = 1: iStartpos = 2
End If
'Worksheet names always end in the "$" character
If Mid$(sTableName, cLength - iTestPos, 1) = "$" Then
sTableName = Mid$(sTableName, iStartpos, cLength - (iStartpos +
iTestPos))
End If

End If

oConn.Close
Set oCat = Nothing
Set oConn = Nothing

SheetsADOX = sTableName
End Function




--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"ChaunceyMo " wrote in message
...
Hi all,

I am importing excels into code via an adodb connection, but have run
into the problem of many of the excels not having standard sheet names
(ie- "import" instead of "Sheet1"). Is there a way to do a SELECT
query without knowing the name of the sheet? This is further
complicated by the workbooks potentially having two sheets rather than
just one, again with an unknown name.

A method that iterates through the sheets sequentially would be
terrific.

Thanks in advance,
Mo


---
Message posted from http://www.ExcelForum.com/




onedaywhen

Querying sheet of unknown name
 
Using the Connection object's OpenSchema method:

Sub test()
Dim Con As Object
Dim rs As Object
Set Con = CreateObject("ADODB.Connection")
With Con
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Tempo\db.xls;" & _
"Extended Properties=Excel 8.0"
.Open
Set rs = .OpenSchema(20) ' adSchemaTables
End With
With rs
Do While Not .EOF
Debug.Print !TABLE_NAME
.MoveNext
Loop
.Close
End With
Con.Close
End Sub

--

ChaunceyMo wrote in message ...
Hi all,

I am importing excels into code via an adodb connection, but have run
into the problem of many of the excels not having standard sheet names
(ie- "import" instead of "Sheet1"). Is there a way to do a SELECT
query without knowing the name of the sheet? This is further
complicated by the workbooks potentially having two sheets rather than
just one, again with an unknown name.

A method that iterates through the sheets sequentially would be
terrific.

Thanks in advance,
Mo


---
Message posted from http://www.ExcelForum.com/



All times are GMT +1. The time now is 11:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com