Hi,
I have a situation where I am encountering the runtime error 1004 only on every OTHER time I run my macro.
Quick background: I'm making a report for work. They want the report in a tabbed-spreadsheet format. I have Access functions with VBA code to run and export the query to Excel, move "Issue Types" to their own dedicated tab, and perform a lot of formatting, close Excel and sent the report as an email attachment. Which all works fine, however it is contingent upon this next part executing successfully
The code below is the one piece that I cannot resolve. It errors at the line I've bolded in red when I attempt to run it a 2nd time (the report needs to be sent multiple times per day). On the 3rd time it works again, not on the 4th, and so on. I'm testing this on the same file, so there is no difference in the file between the multiple attempts.
Code:
Function TEST()
Dim XLAPP As Excel.Application
Set XLAPP = CreateObject("Excel.Application")
XLAPP.Visible = True
'opens exported query (Excel)
XLAPP.Workbooks.Open "M:\New Business - WC\New Business\WC_New_Case_Tracking\RR_Exceptions_Report.xlsx", True, False
Set XLAPP = Nothing
'inserts blank lines between different "Type" values in column A
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 3 Step -1
If Cells(i, "A") <> Cells(i - 1, "A") Then Rows(i).Insert xlShiftDown
Next I
End Function
Column A in my spreadsheet is for "Issue Type", of which there are 5. The code is supposed to insert a blank line in between rows when it sees a change in the "Issue Type".
Again this is working every other time I run it. First attempt executes flawlessly, but the second attempt gives the error UNLESS I close out of the database and go back in. I know this error can occur when objects aren't called out specifically... but I'm really thrown off by it working one time, but not the next.
Any ideas? Thanks!