I am receiving an excel(Book1.xls) file from the user and sometimes one of the worksheets name is XYZ and sometimes its name is XYZ1.
I am doing
Dim Conn,Rs
Set Conn = CreateObject("ADODB.Connection")
Conn.ConnectionTimeout = 10
Conn.CommandTimeout = 300
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Book1.xls;Extended Properties=""Excel 12.0;HDR=No;IMEX=1"";"
Set RS = Conn.Execute("SELECT * FROM [XYZ$B1:B1]")
and when the worksheets name is XYZ1, the last line in the above code is blowing up with an error message: The Microsoft Access database engine could not find the object 'XYZ$CB1:B1'. Make sure the object exists and that you spell its name and the path name correctly. If 'XYZ$CB1:B1' is not a local object, check your network connection or contact the server administrator.
Instead of the last line I would like to do something like
sheetNameString = Conn.Execute("SELECT Join(SheetName) from Book1.xls")
If InStr(sheetNameString,"XYZ1") Then
Set RS = Conn.Execute("SELECT * FROM [XYZ1$B1:B1]")
Else
Set RS = Conn.Execute("SELECT * FROM [XYZ$B1:B1]")
End If