|
-
Sep 30th, 2005, 04:02 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Multiple spreadsheet updates
Ok, this is just bizarre and has been driving me crazy all day.
My program makes changes to 4 different spreadsheets when a button is clicked. It opens sheet 1, changes it, saves it, closes Excel. Then launches excel, opens sheet 2, changes it, saves it, closes excel. Etc.
I wrote the code for updating each sheet as its own block of code, and tested each block of code seperately as I wrote it. (I didn't want to have three sheets open each time I wanted to test the code for the third sheet, etc) I would cut the code for the other parts out and put it into an uncalled temp subroutine at the end of the program while I wasn't using it, and had only the code for the sheet I was working on in the Command1_Click() event.
Each block of code for each of the four sheets works flawlessly as long as it is the only code block in the Click() event. But as soon as I start putting 2, 3, or all 4 blocks together in the Click() event, they start doing weird things, like telling me the sheet already exists and asking if I want to overwrite (the sheets all do exist, and I was not asked to overwrite in each block because I used Application.DisplayAlerts to turn of fthe prompt when saving), or telling me Method 'Cells' of object '_Global' failed on the line:
iRow = Cells.SpecialCells(xlCellTypeLastCell).Row
when no error occurs if the code block is alone.
I'm not sure why this is happening, and why all the code blocks work fine alone but don't play nice together. The code for them all is virtually identical as far as how they call up Excel, open the sheets, activate a sheet, put stuff in the cells, save the sheet, close excel, and do cleanup.
I'm thinking it might be because it's opening up a second instance of Excel while the first instance isn't quite closed yet? I've tried adding a pause in the program between the closing of excel and the opening of it again, but that doesn't seem to help, either using the Sleep API function or a simple for..next loop.
-
Sep 30th, 2005, 04:15 PM
#2
Lively Member
Re: Multiple spreadsheet updates
I had some weird problems like this a while back, ended up to fix the problem Open excel - then the document, alter - close the document and move on to the next, keeping excel open in all cases.
Code:
Dim oExcel as Excel.Application
if oExcel = Nothing
oExcel = CreateObject("Excel.Application")
end if
You can then use oExcel.visible = false if you want to hide it
I am not suffering from insanity......... ...........I am loving every minute of it.
-
Sep 30th, 2005, 04:32 PM
#3
Thread Starter
Addicted Member
Re: Multiple spreadsheet updates
Ok, so would I put that in each code block after the first? And that would replace
Set oExcel = New Excel.Application ?
-
Sep 30th, 2005, 06:26 PM
#4
Re: Multiple spreadsheet updates
If your going off of the code from the other thread the the app object is only created once upon form load. You should only need to open/close workbooks, etc.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 30th, 2005, 06:30 PM
#5
Thread Starter
Addicted Member
Re: Multiple spreadsheet updates
Yes, I found that out when I tried sticking his code in there. I got rid of all the app objects after the first one, and got rid of the oExcel.close and oExcel=Nothing lines in the cleanups. So far, initial tests show it's working, with no more error messages! 
I can't thank all you guys on this Forum enough for your help on this project. Without VBForums and you all who post here, this project would never have been, or would have taken a LOT more time than I probably have in order to finish. When I'm done, can I put up screen shots of my project so you can see what I've done with your help?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|