|
-
Jul 8th, 2005, 08:29 AM
#1
Thread Starter
Member
[Resolved] Simple Excel Request
My code works quite well, the first time it's run. Every subsequent time it screws up.
All I have to do is hit the reset button in the VBA editor to refresh my code, which is fine for me, but the users won't have the knowledge to do that.
Can I add code to make it so that the first or last thing my sub does is the equivalent of hitting that "Reset" button?
What else can I do?
Thanks for your help.
Last edited by Bartender; Jul 11th, 2005 at 09:03 AM.
-
Jul 8th, 2005, 08:36 AM
#2
Re: Simple Excel Request
Er - if your code is erroring after working fine then you are doing something wrong somewhere.
Perhaps you can find out where it is going wrong, setting allreferences to null to release memory etc?
By reset you mean the stop button?
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 8th, 2005, 09:12 AM
#3
Thread Starter
Member
Re: Simple Excel Request
Oh, I know I'm doing something wrong somewhere. What is the command to clear all variables, if there is one?
And, yes, the stop button: Play, Pause, and Stop buttons which have the tooltips "Run Sub/UserForm", "Break", and "Reset", respectively. If I could just, in effect, hit that stop button in between each use of the program, I'd be golden---'til the next bug.
-
Jul 8th, 2005, 09:42 AM
#4
Re: Simple Excel Request
Well at hte bottom of your sub, if you have used any references to open other sheets or workbooks, just put
Code:
set <variable> = nothing
Where <variable> is your variable name holding a reference to an object.
Same if you use recordsets or ranges or any object.
The stop button I was going to suggest using the commandbars (office objects) but that button is not available in excel, only vb editor, where I guess you do not want the users to go.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jul 8th, 2005, 09:52 AM
#5
Thread Starter
Member
Re: Simple Excel Request
Right, the users won't have the knowledge or patience to go into the VB Editor.
Is there any command to,say,
Set AllVariables = 0, or nothing?
-
Jul 8th, 2005, 02:39 PM
#6
Re: Simple Excel Request
You need to destroy all objects individually. The string, date, and numeric vars get destroyed when the code execution goes
out of scope or out of the sub.
VB Code:
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Sheet
Dim .Range As Excel.Range
Dim oCell As Excel.Cell
'do your stuff to initialize them and process, etc.
'...
'...
'...
'Clean up...
Set oCell = Nothing
Set oRange = Nothing
Set oSheet = Nothing
Set oWB = Nothing
'...
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 
-
Jul 8th, 2005, 11:48 PM
#7
Lively Member
Re: Simple Excel Request
Sorry to jump in here, but do you have to hit the reset button after running your code because it goes into debug mode? It may be relevant to post your code, especially if it's a recorded code, as there should be no reason why you should have to reset your code all the time because it error's out.
Depending on the type of variables you have involved, unless your creating new instances of applications, etc.. your variables, if declared at procedure level should be empty when the code is finished executing. They may not be because the code appears to me, to be running into an error of some sort.
-
Jul 11th, 2005, 06:55 AM
#8
Thread Starter
Member
Re: Simple Excel Request
Thanks Robb,
I'll try clearing all the variables manually as you suggest.
Justin,
No, the code completes as it should the first time I run it, every time.
In my code I have a 'best case' condition saved, and a 'second best' condition saved. If I run the same code a second time, what HAD been happening was that the best and second best cases were becoming the same number, which, if the best case was not cleared, I guess would happen with my code. Whats happening now is I'm getting an overflow error, because a Rng.Cells.Count becomes 65531 rather than 2-9 like it should be. Strange.
-
Jul 11th, 2005, 07:05 AM
#9
Thread Starter
Member
Re: Simple Excel Request [Resolved]
Justin, Rob, Ecniv,
Thanks for all your help, this is my first resolved thread, so hooray!
When I re-examined the code to reply to Justin, I saw the problem I called "strange" above, and investigated....
It turned out that I HAD coded it so that everything would reset like I thought I had, except I had missed resetting one very important value and so I had:
VB Code:
If ImportantVal=0 Then
'Do many important things
End If
'but it never did this after the first run because ImportantVal had a value persisting in it for some reason
'So I added, right at the end
ImportantVal=0
Exit Sub
And now, it doesn't need to be reset in between.
Now, If only I could get my other issue up and running...
Last edited by Bartender; Jul 11th, 2005 at 08:54 AM.
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
|