+ Reply to Thread
Results 1 to 9 of 9

vba excel - optimizing code

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    vba excel - optimizing code

    I've come across some code that helps to optimize my vba macros.

    It does seem to make a difference in some respects but my problem is that when I run the code, it disables EnableEvents causing all my worksheet change events to stop working. I know this through the immediate window (Ctl-G: ?application.EnableEvents) returning False after every time I run the optimizing code.

    I even tested it by putting an Application.EnableEvents = True after the Code_End, but it still shows that EnableEvents = False. Would someone be so kind to help me understand what is going on here? Or point me to a good KB on this. I get what EnableEvents is supposed to do, but not sure what the whole "EventState" means and is doing. Hard to find info on this.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: vba excel - optimizing code

    Code_End restores EnableEvents to the state it was in when Code_Begin was called. Are events disabled before you call Code_Begin?

    You'd get the issue you describe if you are calling Code_Begin more than once, too.

    You can either make sure you only call Code_Begin and Code_End once. Or if you know you want events to be enabled after Code_End, then you could change this line:
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: vba excel - optimizing code

    I am not trying to nit-pick here, but I want to make sure we are talking in like terms. What you have posted in NO way optimizes your code. What you have posted are in some cases best practices to help your code overall. Optimizing code is taking the code you have written and altering it so it accomplishes the same thing more quickly. You are just wrapping your code in some commands that may impact speed overall but do not improve your actual codes efficiency.

    Having said that, a few things to point out. Some of these commands are generally good to use across the board, like:

    Please Login or Register  to view this content.
    The above is pretty universally helpful, as the majority of code people write will at some point update the contents of a sheet (yes there are exceptions). So it makes sense to include it in most of your code.

    However many of these commands are not "One size fits all" as you are finding. The only reason you would, for example use:

    Please Login or Register  to view this content.
    Is if your code would trigger an event and you do NOT want it to, for example A Worksheet_Change event that changes the sheet. To prevent an endless loop of events you would have the event disable events, make the change, then enable events.

    Otherwise, there is no benefit to using this command.

    The other thing to keep in mind is that if your code breaks via an error and you do not have an error handler, you wont have toggled everything back on.

    Instead of using a blanket routine for open/close, I use a "template" for my subs and functions, like:

    Please Login or Register  to view this content.
    With the above I have a generic error handler in place that will at least prevent a user from being given debug options and also ensure in case of an error screenupdating is turned back on. I store this in my personal workbook and copy it into a sub and then add in what I need from there. Some projects may require disabling events, some will not, some may need disabling alerts, calculations, etc but not all of them will.

    Its also worth pointing out that if a sub calls another sub, and the parent sub disabled screenupdating for example, the child sub doesnt need to as well. If you did disable in the child too, you are just increasing the chances that along the way it doesnt get flipped back on.

    All in all, if you have code that is running slower than you feel it should, you need to debug the code. Time how long parts of it take to run (using timers in VBA). Inspect your code for logical issues, for ways to simplify the approach, etc. There are for example many methods for moving information from one range to another, some are very slow and some are much faster. Which you use depends on the amount of data it will work with and how simply you want to accomplish the task. There is almost never a right/wrong way to do things in VBA. Usually there are 10 different ways that all have a purpose/use case and becoming a better programmer means knowing which tool to use for the job.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  4. #4
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel - optimizing code

    Very cool Zer0Cool. Thanks for the clarification and exhaustive explanation. Exactly the information I was looking for - I'll keep that nomenclature in mind. I do have
    Please Login or Register  to view this content.
    on all my worksheet change events and I do have my generic error handling within all my worksheet change events and most if not all my parent subs.

    I do have quite extensive parent and child subs that are using these command routines, so knowing a little more about this, I will go back and review how I am structuring my "best practices". There are a couple of very complex routines (mainly a date picker form that may be the source of all this) that I am using within my forms where I think I've isolated this problem to. I will post back confirmation once I narrow down where Events keeps getting disabled.

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel - optimizing code

    Thanks Olly. I do in fact have the events enabled before Code_Begin (as far as I can tell), however, I think your point that I may be calling the Code_Begin and Code_End multiple times is something I need to re-evaluate.

    I have a lot of routines that I have coded this within. I have been fairly diligent about ensuring that if I do call Code_Begin, that I make sure upon exit, that Code_End is called. I will review my code in more detail. Obviously I need to educate myself a little more on this. Thanks again.

  6. #6
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel - optimizing code

    I don't know if this may be applicable to my situation since my date picker (uf called from cmdbutton) selection and output is to a text control within another userform. My question is should I change the declaration
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    From Mr. Pearson's site:

    You can declare the EnableEvents as Private if only procedures with that form need to suppress events. However, if you have forms that are programmatically linked together, such UserForm2 adding an item to a ListBox on UserForm1, you should declare the variable as Public and set it for another form with code like the following:
    Please Login or Register  to view this content.
    The primary difference between the EnableEvents property and code shown above and the Application.EnableEvents property is that with a UserForm EnableEvents, all control on the form must have code to exit if EnableEvents is True. In other words, all the form's controls must cooperate and respect the setting of EnableEvents.

    My (partial) code:
    Please Login or Register  to view this content.
    ...and parent sub that calls the function:
    Please Login or Register  to view this content.
    Last edited by terriertrip; 02-21-2018 at 05:55 PM.

  7. #7
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel - optimizing code

    UPDATE:
    Soooo this is confusing me a bit more, but it's working now. After going through both the entire form code and all its child sub/functions there appears to be nothing wrong with the module code (removed all EnableEvent = True/False to test) or the called function (CustomGetDate) - when this function is called in other forms, the routine behaves as intended.

    However, the only way to get this particular sub above (cmdStop) to ensure that the worksheet event fires upon exiting the sub, is to completely remove
    Please Login or Register  to view this content.
    from the sub within the module. If I check the immediate window both before and after the code, it shows that EnableEvents is enabled regardless if I have the declarations in there, or not. I'm not sure if this clarifies anything more, but I guess if it works, I'll take it.

  8. #8
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: vba excel - optimizing code

    Unless I'm missing something you have to pick one. Instead of "Application.EnableEvents = True/False", you would use "Application.EnableEvents = False" at the beginning of you code to prevent events from stopping the code, then "Application.EnableEvents = True" at the end of your code, just before "End Macro", to turn the events back on.

  9. #9
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: vba excel - optimizing code

    I think OP wrote T/F to save the time/space of writing 2 lines.

    Also @terriertrip I think what you really need to do is comb through your code and reconsider where in the code, if at all, you need to disable and enable events. Doing this is highly situational, again only needed when an event will trigger itself or another event in an unwanted way.

    Especially focus on the scope/hierarchy of your enables/disables. Make sure you are disabling/enabling at the narrowest scope you can.

    Ex:

    Please Login or Register  to view this content.
    The above would mean that events are disabled on all 3 called subs too. If all 3 have their own enable/disable it can conflict with the already set status from the main. If hypothetically you only need sub2 to have events disabled, you should only disable/enable in sub2. The above is a simplistic example, if your hierarchy is more complex it increases the possibilities of having conflicts or circumstances for it to be in an unexpected state.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Optimizing code
    By geri_n in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-30-2014, 01:26 PM
  2. Help Optimizing Code
    By tasugie in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-29-2013, 06:12 PM
  3. need help optimizing my code
    By NirXY in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2013, 11:49 AM
  4. Optimizing Code
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2012, 11:20 AM
  5. Optimizing the Vlookup code in VB. Repitive code with different referencing range.
    By raknahs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-23-2010, 05:03 PM
  6. Help Optimizing Code
    By mgaworecki in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-23-2008, 08:26 AM
  7. Optimizing Code
    By Jim Thomlinson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-02-2005, 10:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1