I have an Excel (2010) Workbook with a command button on a Sheet containing the following code:
Private Sub CommandButton1_Click() Dim TestCol As Collection Dim ThisWorkbook As String Dim Msg, Button, Title, Response As String Button = vbExclamation Title = ActiveSheet.Name & “Private Sub CommandButton1_Click()…” ‘ Amend as necessary ThisWorkbook = ActiveWorkbook.Name Set TestCol = GetSheetsNames(ThisWorkbook) Msg = “ThisWorkbook: ” & ThisWorkbook & vbCrLf & _ “TestCol(2): ” & TestCol(2) Response = MsgBox(Msg, Button, Title) End Sub
The above code calls a UDF defined in a Module called “GetSheetsNames(WorkBookName as string) as Collection” containing the following code:
Function GetSheetsNames(WorkBookName As String) As Collection ‘ Creates a collection (Col) of sheet names from a workbook Dim objConn As ADODB.Connection Dim objCat As ADOX.Catalog Dim tbl As ADOX.Table Dim sConnString As String Dim sSheet As String Dim Col As New Collection Dim Msg, Button, Title, Response As String Button = vbExclamation Title = “Module1: Function GetSheetsNames()…” sConnString = “Provider=Microsoft.Jet.OLEDB.4.0;” & _ “Data Source=” & WorkBookName & “;” & _ “Extended Properties=Excel 8.0;” Set objConn = New ADODB.Connection Msg = ” #1: objConn set, WorkBookName: ” & WorkBookName Response = MsgBox(Msg, Button, Title) objConn.Open sConnString Msg = “#2: WorkBookName: ” & WorkBookName Response = MsgBox(Msg, Button, Title) Exit Function
(The function procedure actually continues on to do other things but for the purposes of demonstration is ended after the Msg/Response block).
I developed the above code in a “Test Workbook.xlsm” where it worked perfectly.
I then copied and pasted the above code sets into my “Working Workbook.xlsm” and it now produces the following error after displaying Msg #1: and before attempting to display Msg #2 with the code line “objConn.Open sConnString” highlighted in the VBE.
41914-20150903-GetSheetsNames-Error
I’ve checked Tools>References in VBE and have the following:
41915-20150903-Tools-References
These Tool>References are identical to those in my Test Workbook
By now I’d be tearing my hair out, but there’s not much left so I’ve resisted that temptation, so now I’m seeking assistance from the Gurus who frequent this place. Of course, any and all clues to help find the cause of the error, or better still a solution, will be greatly appreciated.
Cheers
Trevor