I have a vba function which connects to the same workbook using an ADODB Connection. However, it only works for SOME worksheets. If I try to query a worksheet which does exist, it returns an error. However, other worksheets work fine. My code is below:
Function runExcelQuery(wk As Workbook, strQuery As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim connectionString As String
'Setup the connection string for accessing Excel
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & wk.FullName & _
";Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
cnn.Open connectionString
cnn.CommandTimeout = 0
rst.Open strQuery, cnn
Set runExcelQuery = rst
Exit Function