mrmmickle1
Well-known Member
- Joined
- May 11, 2012
- Messages
- 2,461
Background:
I have an Excel Macro Enabled Template that has a number of dynamic reports on it based on different pivot tables...etc. The file has a fair amount of code and a lot of formatting to make it look "professional"....etc. In the Access Database I run an backup data export currently. That has about 13 different worksheets. The challenge is that I now need to export specific queries to specific worksheets in my new macro enabled template. I am having trouble getting this to work right. Can someone please help me to straighten out my code. I have used only one query in this example. (If someone can help me to do it once I can figure the rest out )
This is my current code that does not work right.... I'm trying to avoid going through every record and exporting each field one at a time so I would like to use .CopyRecordSet if possible. The reason I want to do this si because the queries are quite large and may sometimes contain 30,000 + rows...
Code fails on Red Lines... however I would be interested in some feedback. Not sure this is the best way of doing things....
Thank you for any help !
I have an Excel Macro Enabled Template that has a number of dynamic reports on it based on different pivot tables...etc. The file has a fair amount of code and a lot of formatting to make it look "professional"....etc. In the Access Database I run an backup data export currently. That has about 13 different worksheets. The challenge is that I now need to export specific queries to specific worksheets in my new macro enabled template. I am having trouble getting this to work right. Can someone please help me to straighten out my code. I have used only one query in this example. (If someone can help me to do it once I can figure the rest out )
This is my current code that does not work right.... I'm trying to avoid going through every record and exporting each field one at a time so I would like to use .CopyRecordSet if possible. The reason I want to do this si because the queries are quite large and may sometimes contain 30,000 + rows...
Code fails on Red Lines... however I would be interested in some feedback. Not sure this is the best way of doing things....
Code:
[COLOR=#0000ff][B]Sub [/B][/COLOR]ExportToTemplatev1()
[COLOR=#0000ff][B]Dim [/B][/COLOR]xl [COLOR=#0000ff][B]As[/B][/COLOR] Excel.Application
[COLOR=#0000ff][B]Dim[/B][/COLOR] xlFilePath [B][COLOR=#0000ff] As String[/COLOR][/B]
[COLOR=#0000ff][B] Dim[/B][/COLOR] xlWS [COLOR=#0000ff][B]As[/B][/COLOR] Worksheet
[COLOR=#0000ff][B]Dim[/B][/COLOR] rng [B][COLOR=#0000ff] As Range[/COLOR][/B]
[COLOR=#0000ff][B] Dim [/B][/COLOR]i [COLOR=#0000ff][B] As Long[/B][/COLOR]
[COLOR=#0000ff][B] Dim[/B][/COLOR] lFieldCount [COLOR=#0000ff][B] As Long[/B][/COLOR]
[COLOR=#0000ff][B] Dim[/B][/COLOR] rsQuery [COLOR=#0000ff][B]As[/B][/COLOR] Recordset
[COLOR=#0000ff][B]Set[/B][/COLOR] xl = New Excel.Application
xlFilePath = "C:\Users\mmickle1\Documents\SpotlightExportTest.xlsm"
Workbooks.Open xlFilePath
xl.Visible = [COLOR=#0000ff][B]True[/B][/COLOR]
[COLOR=#008000][B] 'Set the worksheet:[/B][/COLOR]
[COLOR=#0000ff][B] Set[/B][/COLOR] xlWS = ActiveWorkbook.Sheets("Sheet1")
[COLOR=#008000][B] 'open query[/B][/COLOR]
[COLOR=#ff0000][B] Set rsQuery = CurrentDb.OpenRecordset("01 HC-Most Recent Quarter", dbOpenDynaset)[/B][/COLOR]
[COLOR=#0000ff][B]Set [/B][/COLOR]rng = ws.Range("A1")
lFieldCount = rsQuery.Fields.Count
[COLOR=#0000ff][B]For[/B][/COLOR] i = 0 [COLOR=#0000ff][B]To[/B][/COLOR] lFieldCount - 1
[B][COLOR=#008000] 'copy column names in first row of the worksheet:[/COLOR][/B]
rng.Offset(0, i).Value = adoRecSet.Fields(i).Name
[COLOR=#0000ff][B] Next[/B][/COLOR] i
[COLOR=#008000][B] 'copy record values starting from second row of the worksheet:[/B][/COLOR]
rng.Offset(1, 0).CopyFromRecordset rsQuery
rsQuery.Close
[COLOR=#0000ff][B]End Sub[/B][/COLOR]
Thank you for any help !