Using variable instead of single file name for export to Excel, based on a form selection

Not open for further replies.


New Member
Apr 2, 2018
Hello all,

There are various subs on the internet which export to an existing Excel file I have tried, and they work fine. But each one I've seen requires the use of a single file name such as SingleFileName.xls

Since my database had dozens of tables, which might be needed by dozens of related existing Excel files, I am trying to have a form button determine a variable string, which in turn will determine which single table in the d/b, and which single Excel file to use.

Every attempt I've tried has failed. For the code attached, I tried using a variable called ProjectID. But VBA errors out saying Compile Error - Object required. I totally lost on what that means. Seems like a variable should work for this code. Would any one know how to use a variable both the Excel file to export to, and use that same variable for the Access table in the d/b? Thank you for any responses.

Again the code works fine if I input a single file name, with a single table name. So the code works fine that way.

Sub ExcelExportFromAccess()

Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
Dim ProjectID As String
Set ProjectID = "Project123456"

blnEXCEL = False
' Replace True with False if you do not want the first row of
' the worksheet to be a header row (the names of the fields
' from the recordset)

blnHeaderRow = True

' Establish an EXCEL application object

On Error Resume Next
Set xlx = GetObject(, "Excel.Application")

If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
On Error GoTo 0

' Change True to False if you do not want the workbook to be
' visible when the code is running
xlx.Visible = True

' Replace C:\Filename.xls with the actual path and filename
' of the EXCEL file into which you will write the data

Set xlw = xlx.Workbooks.Open("C:\Users\pweston\Documents\Access Test\ProjectID & .xlsm")
' Replace WorksheetName with the actual name of the worksheet
' in the EXCEL file
' (note that the worksheet must already be in the EXCEL file)
Set xls = xlw.Worksheets("TotalHistory")
' Replace A1 with the cell reference into which the first data value
' is to be written
Set xlc = xls.Range("A1") ' this is the first cell into which data go
Set dbs = CurrentDb()
' Replace QueryOrTableName with the real name of the table or query
' whose data are to be written into the worksheet
Set rst = dbs.OpenRecordset(ProjectID, dbOpenDynaset, dbReadOnly)

If rst.EOF = False And rst.BOF = False Then
If blnHeaderRow = True Then
For lngColumn = 0 To rst.Fields.Count - 1
xlc.Offset(0, lngColumn).value = rst.Fields(lngColumn).Name
Next lngColumn
Set xlc = xlc.Offset(1, 0)
End If
' write data to worksheet
Do While rst.EOF = False
For lngColumn = 0 To rst.Fields.Count - 1
xlc.Offset(0, lngColumn).value = rst.Fields(lngColumn).value
Next lngColumn
Set xlc = xlc.Offset(1, 0)
End If
Set rst = Nothing
Set dbs = Nothing

End Sub

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"


MrExcel MVP, Moderator
Mar 2, 2007
Office Version
You probably should continue in the same thread, so I will close this one. Put your updated code there.

However, this again looks wrong, for the same reasons as before.
Set xlw = xlx.Workbooks.Open("C:\Users\pweston\Documents\Access Test\ProjectID & .xlsm")
Not open for further replies.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...