I am trying to automate exporting a table from Access into an Excel report. From my research on Stack Overflow, it is recommended to have a VBA code for the export, and then another code for formatting the Excel sheet. I would greatly prefer to run the formatting of the Excel sheet from Access, so I just push a button and the thing is exported. I absolutely hate running this report because everyone wants it "right now" when I have other pressing things to do. It's for the Company President's office, so a peon like me just has to hop to.
I found this suggested code here: https://stackoverflow.com/a/14401307/6850333, but I'm getting an error of "Object variable or With block variable not set"
Sub FormatACTrade(strFilePath As String) 'I'm running this from another _
sub that exports the tables to excel, I know functions are needed for _
an output but this is a private variable that I'm using within a modle_
so should be ok??
On Error GoTo ErrorHandler
Dim objFile As Object
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strFilePath)
If objFso.GetExtensionName(objFile.Path) = "xlsx" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
For Each sh In objWorkbook.Worksheets
If sh.UsedRange.Address <> "$A$1" Or sh.Range("A1") <> "" Then
With sh
[Do stuff]
End With
End If
Next
objWorkbook.Close True
End If
End Sub
Any suggestions are appreciated. I'm new to coding, and I have to admit the documentation on vba objects, scripting, and applications are difficult to comprehend. I understand I am not properly naming an object, but I don't really understand what the code is even doing at this line Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
.