AOB
Well-known Member
- Joined
- Dec 15, 2010
- Messages
- 669
- Office Version
- 365
- 2016
- 2013
- Platform
- Windows
Just discovered this oddity and wondering if anybody knows why it's happening / how to work around it?
I have some VBA in Access that generates an Excel workbook, creates some tables (
Code is pretty straightforward; if it's the first column in the table, it already exists (from when the table is created) so just set the
(I've reduced the code down to the bare essentials here but suffice to say, the code works perfectly fine outside of what I've posted below)
What's weird is, while developing/debugging, for obvious reasons, I make the Excel application visible and enable
This throws a very unexpected (to me?) error :
But it's only an issue if Excel is hidden and the ScreenUpdating is disabled- if I set the application visibility to
(For clarity, I have to do both - making the application visible but keeping
Can't get my head around why this would be an issue, any ideas?
I have some VBA in Access that generates an Excel workbook, creates some tables (
ListObject
) and manipulates them from some queries / recordsets. (The pertinent point being, this code is running "remotely" from Access, using the Excel Object Model, late-bound, rather than within Excel natively... Which I do all the time, I'm well used to it at this stage...)Code is pretty straightforward; if it's the first column in the table, it already exists (from when the table is created) so just set the
ListColumn
object to that first ListColumn
and modify as necessary. If it's not the first column in the table, add a new ListColumn
and modify that, etc.(I've reduced the code down to the bare essentials here but suffice to say, the code works perfectly fine outside of what I've posted below)
What's weird is, while developing/debugging, for obvious reasons, I make the Excel application visible and enable
ScreenUpdating
. Now that I have everything working as expected, I keep the Excel instance hidden and disable ScreenUpdating
while it's doing all the work. Now, it bugs out when it hits the line as commented :
VBA Code:
Dim sht As Object ' Worksheet object
Dim lso As Object ' Table object
Dim lsc As Object ' ListColumn object
With sht
Set lso = .ListObjects.Add
With lso
' Loop through the report columns for the report and add them from the raw data table
For j = 0 To lngColumnCount - 1
Select Case j
Case 0
Set lsc = .ListColumns(1)
Case Else
Set lsc = .ListColumns.Add ' This is where the error occurs
End Select
' Work with the list column...
Next j
End With
End With
This throws a very unexpected (to me?) error :
Error 1004 : Application-defined or object-defined error
But it's only an issue if Excel is hidden and the ScreenUpdating is disabled- if I set the application visibility to
True
, and enable ScreenUpdating
, it all works fine?(For clarity, I have to do both - making the application visible but keeping
ScreenUpdating
disabled, still throws the error; keeping the application hidden but enabling ScreenUpdating
disabled, also throws the error)Can't get my head around why this would be an issue, any ideas?