AOB
Well-known Member
- Joined
- Dec 15, 2010
- Messages
- 669
- Office Version
- 365
- 2016
- 2013
- Platform
- Windows
I have some code that generates a table in Excel (ListObject) from a query. As part of the process, I need to delete a couple of redundant columns.
But when I run the script (the script is running "remotely" from Access, not in Excel directly) the
If, however, I add a breakpoint to the code just before I attempt to delete the columns, switch focus to the Excel workbook (i.e. make it visible), then go back and resume the code, it works fine?
The same thing happens if I try to add a new ListColumn to a table :
Again, the
How can I manipulate the table(s) without having to interrupt? The whole point of the code is to automate the generation of the Excel workbook "in the background" and just present the final result.
All suggestions welcome! Thanks!
VBA Code:
Dim objWorkbook As Object
Dim objWorksheet As Object
Dim objTable As Object
Set objWorksheet = objWorkbook.Sheets("MyWorksheet")
Set objTable= objWorksheet.ListObjects("tblMyTable")
objTable.ListColumns("ColumnNameA").Delete
objTable.ListColumns("ColumnNameB").Delete
But when I run the script (the script is running "remotely" from Access, not in Excel directly) the
.Delete
lines throw 1004 errors :Error 1004: Application-defined or object-defined error
If, however, I add a breakpoint to the code just before I attempt to delete the columns, switch focus to the Excel workbook (i.e. make it visible), then go back and resume the code, it works fine?
The same thing happens if I try to add a new ListColumn to a table :
VBA Code:
Dim objListColumn1 As Object
Dim objListColumn2 As Object
Set objListColumn1= objTable.ListColumns("SomeColumn")
Set objListColumn2= objTable.ListColumns.Add(objListColumn1.Index + 1)
Again, the
.Add
command only works if I pause the code, make the Excel worksheet visible, then resume the code.How can I manipulate the table(s) without having to interrupt? The whole point of the code is to automate the generation of the Excel workbook "in the background" and just present the final result.
All suggestions welcome! Thanks!