Excel VBA to create Cross Tab Table

carrilmm

New Member
Joined
Sep 30, 2015
Messages
1
Good Day,

I am relatively new to the VBA coding arena so please bare with my ignorance.

I am trying to apply some code I found on the internet to take some data from an excel table and create the equivalence of a cross tab query in another excel worksheet within the same workbook. Essentially I am trying to calculate the average of a data value with in the table and to aggregate this information by end user and month. Here is the code I am using:

Sub ApplyCrossTab()
Dim Myconnection As ADODB.Connection
Dim Myrecordset As ADODB.Recordset
Dim Myworkbook As String
Dim strSQL As String
Dim i As Integer

Set Myconnection = New Connection
Set Myrecordset = New Recordset

'Identify the workbook you are referencing
Myworkbook = ActiveWorkbook.Sheets("Completions").Select

'Open connection to the workbook
Myconnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Myworkbook & ";" & _
"Extended Properties=""Excel 12.0;HDR=YES;"";"
'& _
'"Persist Security Info=False"

'Build SQL Statement
strSQL = "TRANSFORM Avg(CycleTime) " & _
"SELECT cust_nm_top, Product " & _
"FROM Completions " & _
"GROUP BY cust_nm_top, Product " & _
"PIVOT Month"

'Load the Query into a Recordset

Debug.Print strSQL
Myrecordset.Open strSQL, Myconnection, adOpenStatic, adLockOptimistic, adCmdText


'Place the Recordset onto Sheet2
With ActiveWorkbook.Sheets("Sheet2").Range("A1")
.CopyFromRecordset Myrecordset
End With

'Place the Recordset onto Sheet2
With Sheets("Sheet2")
.Range("A2").CopyFromRecordset Myrecordset

'Add column heading names
For i = 1 To Myrecordset.Fields.Count
.Cells(1, i).Value = Myrecordset.Fields(i - 1).Name
Next i

End With

End Sub

When I attempt to run the code I receive the following error box

Run-rime error '-2147217865(80040e37)':

The Microsoft Access database engine could not find the object 'Completions'.

If I use the Provider=Microsoft.Jet.OLEDB.4.0, I also receive the same error.

Any help would be greatly appreciated.
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,221,418
Messages
6,159,795
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top