Prattdakota
Vendor
I'm having an issue while trying to edit an Excel workbook from Access using VBA. The odd thing is, when I open the database and run the routine the first time it works. When I try to run it a second time I get a "run time error 424 Object required" message. When I go to debug, the program has stopped on the "For Each c In Selection" line of code below. I assumed that I was not cleaning something up correctly since it worked the first time but not subsequent times. Setting the objects to "nothing" does not seem to help.
After I run the routine the first time Excel does save and close itself properly and so is not open when I run the routine the second time.
Any assistance would be appreciated.
After I run the routine the first time Excel does save and close itself properly and so is not open when I run the routine the second time.
Any assistance would be appreciated.
Code:
Sub FormatExport(strFileName As String)
Dim objExcelApp As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Set objExcelApp = New Excel.Application
With objExcelApp
.Visible = True
Set objWorkbook = .Workbooks.Open(DBPath & "Template", , False)
End With
Set objWorksheet = objWorkbook.Sheets(1)
objWorksheet.Activate
objWorksheet.Rows("1:1").Select
objExcelApp.Selection.Delete
objWorksheet.Range("B5:M100").Select
For Each c In Selection
c.Value = c.Value
Next c
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
objWorkbook.Close True, strFileName
objExcelApp.Quit
Set objExcelApp = Nothing
Set objWorkbook = Nothing
Set objWorksheet = Nothing
End Sub