+ Reply to Thread
Results 1 to 9 of 9

VBA Autohide/Unhide Execution Speed

  1. #1
    Registered User
    Join Date
    09-01-2015
    Location
    ia
    MS-Off Ver
    2007
    Posts
    12

    VBA Autohide/Unhide Execution Speed

    Good afternoon all. I've created a workbook with multiple tabs that utilize a similar auto-hide/unhide VBA that constantly runs to expand or minimize choices based on previous user selections. A couple of my spreadsheets have up to 50 some rows that are included in my hide/unhide. The issue that I'm running across is how to speed up the VBA as it executes each time the user selects a response. Below is an excerpt that I'm using across each workbook tab. Is there anything I've currently got entered that would expand the time to run the code? Like I said, I need to code to continuously run, but wasn't sure if I have it properly constrained to search only a select portion of Column A or if I have it done to search the entire column A of the entire worksheet. Any help would be greatly appreciated. Note: this particular VBA does have a sheet unlock/lock function embedded as I've having issues protecting the sheet by normal means and having the VBA execute.


    HTML Code: 

  2. #2
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: VBA Autohide/Unhide Execution Speed

    Try this.
    - Turn off screen redrawing during the macro's execution. See the bold instructions in the code module below.
    This will be the main time saver I think.

    Other tips:
    - The IF decision can be simplified (no ELSEIF decision is needed) although that's not as significant.
    - The value of variable LastRow is not being used in the macro so it's calculation line could be removed.
    Or perhaps you want For Each c in Range("A6:A" & LastRow) to start your loop. You'll have to decide.
    - The entire IF/ELSE/END structure could be written as c.EntireRow.Hidden = (c.value<>"X").
    I myself prefer your method though.
    - A more complex method is to build an array of row numbers to be hidden, and then hide them all at once after the loop. (Not shown here)
    - One other suggestion is to make all the rows visible before the loop (done quickly), and then hide only the ones without an "X".
    This is what is shown below.

    Please Login or Register  to view this content.
    If this has been helpful, please click on the star at the left.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Autohide/Unhide Execution Speed

    The Worksheet_Calculate event procedure is probably not the best way to do this.

    Quote Originally Posted by xtremca View Post
    The issue that I'm running across is how to speed up the VBA as it executes each time the user selects a response.
    It's probably best to use the Worksheet_Change event procedure and trigger off of whatever cell the user is changing. This example uses cell A1 as the user-triggered change and auto filters from A6 to the last used cell in column A. (Remember to comment out the Worksheet_Calculate procedure).

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    09-01-2015
    Location
    ia
    MS-Off Ver
    2007
    Posts
    12

    Re: VBA Autohide/Unhide Execution Speed

    I'm not sure if the screen updating on my larger worksheets, but maybe it will help with users who may have reduced processing power.

  5. #5
    Registered User
    Join Date
    09-01-2015
    Location
    ia
    MS-Off Ver
    2007
    Posts
    12

    Re: VBA Autohide/Unhide Execution Speed

    Quote Originally Posted by AlphaFrog View Post
    The Worksheet_Calculate event procedure is probably not the best way to do this.



    It's probably best to use the Worksheet_Change event procedure and trigger off of whatever cell the user is changing. This example uses cell A1 as the user-triggered change and auto filters from A6 to the last used cell in column A. (Remember to comment out the Worksheet_Calculate procedure).

    Please Login or Register  to view this content.
    If I recall, I had some issue with worksheet_change not updating the auto hide/unhide on my A column fields that had a formula driven response. This is what led me to the worksheet_calculate. My A column "X" can be dependent on a previous A column X or dependent on and If(and of a previous A column response and item code (column E on the attached). I've attached a sample worksheet similar to my project to show the A column formulas, etc. On the attached, Column A and E are normally hidden from the user as it's just used for my lookups and auto sorting.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Autohide/Unhide Execution Speed

    Quote Originally Posted by xtremca View Post
    If I recall, I had some issue with worksheet_change not updating the auto hide/unhide on my A column fields that had a formula driven response. This is what led me to the worksheet_calculate. My A column "X" can be dependent on a previous A column X or dependent on and If(and of a previous A column response and item code (column E on the attached). I've attached a sample worksheet similar to my project to show the A column formulas, etc. On the attached, Column A and E are normally hidden from the user as it's just used for my lookups and auto sorting.
    Again, use the User-changed cells in column C to qualify the Worksheet_Change trigger and not the column A formulas. Once the trigger is qualified as column C user-changed, then use the column A formula results to determine which rows to hide\unhide.

    Something like...

    Please Login or Register  to view this content.
    Reminder: don't use both this and the Worksheet_Calculate procedure.

  7. #7
    Registered User
    Join Date
    09-01-2015
    Location
    ia
    MS-Off Ver
    2007
    Posts
    12

    Re: VBA Autohide/Unhide Execution Speed

    Quote Originally Posted by AlphaFrog View Post
    Again, use the User-changed cells in column C to qualify the Worksheet_Change trigger and not the column A formulas. Once the trigger is qualified as column C user-changed, then use the column A formula results to determine which rows to hide\unhide.

    Something like...

    Please Login or Register  to view this content.
    Reminder: don't use both this and the Worksheet_Calculate procedure.
    Not sure what I was doing before that I had such an issue with the WKsheet_change, but it's definitely gone turbo speed. Now my only issue is that I was already using a wksheet_change to make certain cells default back to a position so if the user made a change to a prior selection, it would make a corresponding selection menu default from a choice to "please select".

    Here is the code including what you have provided me along with my auto resets. How do I go about combining these so they can both be used since as if I'm thinking correctly, you cannot stack more than one worksheet_change at one time. (at least separated)

    Please Login or Register  to view this content.
    Last edited by xtremca; 11-25-2015 at 03:22 PM.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA Autohide/Unhide Execution Speed

    You're welcome.

    Not having the benefit of seeing your actual worksheet, I'm guessing it may be something like this...

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-01-2015
    Location
    ia
    MS-Off Ver
    2007
    Posts
    12

    Re: VBA Autohide/Unhide Execution Speed

    I'll work on updating the code on all my tabs. If I run into any issues, I'll let you know. Otherwise, thanks again for the help and have a great holiday weekend.

+ 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. execution speed comparrision
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-27-2015, 01:40 PM
  2. Speed up this macros execution
    By banaanas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2015, 01:58 AM
  3. autohide unhide rows
    By vikram251197 in forum Excel General
    Replies: 1
    Last Post: 01-09-2015, 10:32 AM
  4. Want to Speed Up Code Execution
    By PosseJohn in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-28-2013, 07:34 PM
  5. Execution speed of macros
    By JM967 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2013, 02:43 PM
  6. [SOLVED] 2010VBA AutoHide(&unhide) rows based on dynamic cell value
    By epicurean in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2013, 01:30 PM
  7. [SOLVED] Speed up Excel execution
    By Sinus Log in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-08-2005, 01:10 AM

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