I have an interesting problem. I'm running a SQL query and then populating an Excel Spreadsheet(s). After completing the reading of the SQL output I do some calculation on the spreadsheets and then give the operator the option to save or not the spreadsheet. Then I return to the form that ran the VBA code. The problem is Excel doesn't seem to unload and clear memory. If I enter the options to run another extract I get an error on the Activesheet.Name!!
The error is "run-time error '91': Object variable or With block variable not set"
The first time through everything is fine. The 2nd time is when I get the error.
On opening I have the following:'
Set XL = New Excel.Application
XL.Visible = True
Set wb = XL.Workbooks.Add
Set WKS = wb.Worksheets(1)
And on closing I have the following:
Set wb = Nothing
XL.Quit
Set XL = Nothing
I've checked the Task Manager and apparently Excel is gone at the end of the first pass.
It appears that it thinks that Excel is still there or it isn't setting up a new workbook for the second pass. I'm not sure which.
Any help would be appreciated. The coding is very long and too big to post it all here but the app seems to be working fine other than running it multiple times in a row.