02/07/2019 Export Access data to Excel using VBA (with Images & Code)
VBA Code to Export Access data to Excel
Anson Automation, VBA Automation
Export Access data to Excel using VBA (with Images & Code)
What is it?
This VBA code helps you to export data in Access Table to Excel in one click.
Why is it?
You can upload or export Access data to Excel without even opening the Excel Workbook.
How to Export Access data to Excel
Step 1: Open the Access DB and got to VBA code builder (Alt + F11) and Open a New Module.
Step 2: Copy and Paste the below code in it.
Sub exportToXl()
On Error GoTo ErrorHandler
Dim dbTable As String
Dim xlWorksheetPath As String
xlWorksheetPath = “C:\” ‘Mention the xlWorkbook path
xlWorksheetPath = xlWorksheetPath & “xlWorkbookName.xlsx” ‘Replace the ‘xlWorkbookName.xlsx’ with your workbook name
dbTable = “tblMaster” ‘Replace ‘tblMaster’ with the table in the Access DB from which you want to export the data
DoCmd.TransferSpreadsheet transfertype:=acExport, spreadsheettype:=acSpreadsheetTypeExcel12, tablename:=dbTable, FileName:=xlWorksheetPath, hasfieldnames:=True
ErrorHandlerExit:
Exit Sub
https://www.askeygeek.com/vba-code-to-export-access-data-to-excel/ 1/3
02/07/2019 Export Access data to Excel using VBA (with Images & Code)
ErrorHandler:
MsgBox “Error No: ” & Err.Number & “;Description: ” & Err.Description
Resume ErrorHandlerExit
End Sub
Step 3: Click the Run button or F5 or the Command button to which you have associated this code!
In case you are assigning the code to a Command button make sure the code is within the Private/Public Sub_Event(); Something like the below
Private Sub exportToXl_Click()
Access DB records have been successfully uploaded to Excel, Open your Excel workbook and check the data.
Note: Make sure ‘Microsoft ActiveX Data Objects Library’ is enabled from the Tools – References (use latest version [6.1 as of this post]).
https://www.askeygeek.com/vba-code-to-export-access-data-to-excel/ 2/3
02/07/2019 Export Access data to Excel using VBA (with Images & Code)
About Anson
I’m Anson having a decade of versatile experience in Business Process Outsourcing, Finance & Accounting, Information Technology, Operational Excellence & Business Intelligence. I have got the opportunity to work for Genpact, Hewlett Packard, M*Modal and Capgemini in
various roles and responsibilities, starting from an Associate to a Manager. Learning anything new has always been my passion and if related to Business and Technology then I love it. asKeygeek.com is an outcome of my passion towards the same and I have been into asKeygeek
since 2012. Outside business and technology I’m a movie buff who spends hours together watching and learning Cinema, I do make Short Films
View all posts by Anson →
https://www.askeygeek.com/vba-code-to-export-access-data-to-excel/ 3/3