Results 1 to 9 of 9

Thread: [Resolved] Simple Excel Request

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    Resolved [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.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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?

    BOFH Now, BOFH Past, Information on duplicates

    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...

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    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.

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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.

    BOFH Now, BOFH Past, Information on duplicates

    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...

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    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?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Dim oWB As Excel.Workbook
    2. Dim oSheet As Excel.Sheet
    3. Dim .Range As Excel.Range
    4. Dim oCell As Excel.Cell
    5.  
    6. 'do your stuff to initialize them and process, etc.
    7.  
    8. '...
    9. '...
    10. '...
    11.  
    12. 'Clean up...
    13. Set oCell = Nothing
    14. Set oRange = Nothing
    15. Set oSheet = Nothing
    16. Set oWB = Nothing
    17. '...
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    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.
    Justin Labenne
    www.jlxl.net

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    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:
    1. If ImportantVal=0 Then
    2. 'Do many important things
    3. End If
    4. 'but it never did this after the first run because ImportantVal had a value persisting in it for some reason
    5. 'So I added, right at the end
    6. ImportantVal=0
    7. 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
  •  



Click Here to Expand Forum to Full Width