VBA: Need to Add Access Query Headers to Excel Export

shlobodon

New Member
Joined
Oct 15, 2015
Messages
44
Hello,

I have a macro that moves an Access query to Excel. I am unable to bring the headers from that query into Excel, but I need to for organizing purposes.

This is the working code that does everything but bring in headers.

Option Compare Text
Sub diversityauto()
StrDBPath = "K:\nataccts\Rob D\Diversity Report Automation\Supplier Diversity Report Clone.accdb"
Windows("Diversity Report List.xlsx").Activate
Sheets("Report List").Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Set costsavings = Workbooks.Open("K:\nataccts\Rob D\Diversity Report Automation\Diversity Supplier Report_Template.xlsx")
Windows("Diversity Report List.xlsx").Activate
Sheets("Report List").Select
blnHeaderRow = True
CBA1 = Cells(i, "C").Value
Set con = New ADODB.Connection
With con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open StrDBPath
End With
sSQL = "TRANSFORM Sum([Spend by Supplier].[Sales Amount]) AS [SumOfSales Amount] SELECT [Spend by Supplier].[Cal year / month] FROM [Spend by Supplier] INNER JOIN [2015 Diversity File Compacted] ON [Spend by Supplier].[Supplier Code] = [2015 Diversity File Compacted].[Vendor No] WHERE [Spend by Supplier].CBA1 GROUP BY [Spend by Supplier].[Cal year / month] PIVOT [2015 Diversity File Compacted].[Diverse Category];"
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseServer
rs.Open Source:=sSQL, ActiveConnection:=con, CursorType:=AdForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText
Windows("Diversity Supplier Report_Template.xlsx").Activate
Sheets("Diversity Summary by Month").Select
Range("B33").CopyFromRecordset rs
rs.Close
con.Close
Next i
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You need to loop through the Fields collection of the recordset rs to get the field names.[
Code:
With Sheets("Diversity Summary by Month")

    Set rngHdrs = .Range("B32")

    For Each fld In rs.Fields
        rngHdrs.Value = fld.Name
        Set rngHdrs = rngHdrs.Offset(,1)
    Next fld

    .Range("B33").CopyFromRecordset rs

End With
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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