Access Report to Excel Template

natedawg1415

New Member
Joined
Mar 1, 2017
Messages
1
Here is some code you can use.
Replace the template name and query name in the code and set the starting range (now A1).
This will create a new workbook based on your template (which actually doesn't need to be a real template, you can use any excel file) and then prompts the user to save the file after pasting the data in the disered range.

Code:
Public Sub CreateExcelInfo()
'Set reference to Microsoft Excel Object library
'Set reference to Microsoft ActiveX DataObject 2.x
Const sFileNameTemplate As String = "PathAndNameOfYourTemplateXLSX"
Dim oExcel As New Excel.Application
Dim WB As New Excel.Workbook
Dim WS As Excel.Worksheet
Dim rng As Excel.Range
Dim objConn As New ADODB.Connection
Dim objRs As New ADODB.Recordset
Dim vData As Variant
Dim sSQL As String
Set objConn = CurrentProject.Connection
sSQL = "Select * from qTheQuery" 'This has to be the name of the query your report is using to display data
With objRs
    .Open sSQL, objConn, adOpenStatic, adLockReadOnly
    vData = .GetRows()
    .Close
End With
With oExcel
    .Visible = True
               'Create new workbook from the template file
                Set WB = .Workbooks.Add(sFileNameTemplate)
                With WB
                     Set WS = WB.Worksheets("Sheet1") 'Replace with the name of actual sheet
                     With WS
                              Set rng = .Range("A1") 'Starting point of the data range
                              rng.Resize(UBound(vData, 2) + 1, UBound(vData, 1) + 1).Value = oExcel.WorksheetFunction.Transpose(vData)
                     End With
 
                End With
 
    .Quit
End With
 
'clean up
Set oExcel = Nothing
Set objRs = Nothing
Set objConn = Nothing
Set vData = Nothing
End Sub

You can create a button on the report to trigger this code.


When I do this I get a Run-time Error 13 type mismatch. Could this be to me using office 2016? when I click the debug icon it highlights the line "rng.Resize(UBound(vData, 2) + 1, UBound(vData, 1) + 1).Value = oExcel.WorksheetFunction.Transpose(vData)" What could cause this?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Could this be to me using office 2016?
No. The error means you are passing a value to something (e.g. an object, variable, sql statement, etc) that is of the wrong data type. This often happens when you try to pass text where a number is expected, or Null where it's not allowed, or a date where text or a number is expected, etc. etc. I can't help to pinpoint the exact problem since I've never dabbled in that sort of procedure. However, now that you know what the error means, you know what to look for. You could put a break point on that line and use the immediate window to test what the variable values are (such as vData) and compare them to what you're expecting.
 
Upvote 0

Forum statistics

Threads
1,221,710
Messages
6,161,445
Members
451,706
Latest member
SMB1982

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