+ Reply to Thread
Results 1 to 13 of 13

Help improving performance?

  1. #1
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Help improving performance?

    I have a VBA function that's running very slowly, and I'm looking for suggestions as to how to improve it. The context is that the sheet has multiple sets of rows called 'components', and this function is walking through them determining which ones should be visible and which should be hidden

    Here's my (probably naive) implementation:

    Please Login or Register  to view this content.
    I don't think the implementation of reset_component or hide_or_show_component matters much, but I'll post them if they might.

    Anybody have any suggestions?

    Thanks,
    Aaron

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help improving performance?

    Hi,

    Rather than loop through zillions of cells just get the macro to autofilter your data using the appropriate criteria. Preface this bit of the macro by switching off any autofilter that may already be in place,

    As a general rule always make use of standard Excel functionality if you can. Without exception it is always quicker than VBA code.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help improving performance?

    See how AutoFilter improved some code here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    Quote Originally Posted by Richard Buttrey View Post
    Hi,
    Rather than loop through zillions of cells just get the macro to autofilter your data using the appropriate criteria.
    Thanks, that's very useful. Is there a way to do something similar for a property other than Visible? Specifically, I'd like to set some of the components to "disabled" by changing their text color to gray:

    Please Login or Register  to view this content.
    Thanks,
    Aaron

  5. #5
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    Got it - I need to combine conditional formatting (for the gray text) with filtering (to hide the appropriate rows).

    Aaron

  6. #6
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    OK, I thought I had it, but apparently not. Conditional formatting, as far as I can tell, is utterly and completely broken? I can apply the same rule to the same selection twice in a row and get two different results. What the hell?

    Assuming that I can get it working manually, there seems to be an interaction between conditional formatting and filtering? Here's the code that I'm hoping to use:

    Please Login or Register  to view this content.
    Does this look like it ought to work? Column BC contains a value "TRUE" or "FALSE" indicating whether the row should be hidden; column BD contains a similar value indicating whether the row should be grayed.

    Thanks,
    Aaron

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Help improving performance?

    You can't use a colour set by Conditional Formatting, you need to use the actual condition

  8. #8
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    I'm sorry, I didn't understand that. I can't use a color set by Conditional Formatting for what?

    Aaron

  9. #9
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    The problem seems to have to do with my currently-selected cell, which suggests that UsedRange doesn't do what I expected it to. If my currently-selected cell is A2 when the code runs, all my formatting is shifted down a row. That is, row 4 gets the formatting from row 3, and so forth.

    Any idea how this code could be affected by the current selection?

    Thanks,
    Aaron

  10. #10
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    OK, so it isn't UsedRange that's behaving strangely - it must be the $BD1 reference in the conditional formatting formula. Given that I'm applying the formatting to UsedRange, which starts at A1, shouldn't that use the value of column BD for every row, regardless of the current selection at the time the macro runs?

    Thanks,
    Aaron

  11. #11
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    OK, here's a clue: When I run the code (with the active cell set to A3), and then look at the formatting rules applied to any random cell, I see "=$BD65535=FALSE". Oddly, even though every cell has the same formula showing up with the same row number, some rows end up formatted and some don't.

    I stand by my statement that conditional formatting is broken, unless somebody can point out what I'm doing wrong.

    Thanks,
    Aaron

  12. #12
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    OK, I think I've got it working by a collection of funky hacks and workarounds. I have to manually hide row 1 because for some reason the filter isn't willing to hide it, and I have to mess with the selection in the code or the conditional formatting doesn't work. Here's the final code - I'd appreciate any ideas for removing some of the cruft.

    Please Login or Register  to view this content.
    Thanks,
    Aaron

  13. #13
    Registered User
    Join Date
    01-20-2011
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Help improving performance?

    OK, after all that, I don't think I'm going to be able to use that technique. It's a good one to know, but the problem is that I would have to add a "visible" flag to each row. The flags would have to be hidden, which means that if the user inserts a row, it won't have a flag, and they won't know why it's not working. I'm putting a lot of effort into making this sheet difficult for users to damage, so that's not an acceptable compromise. I tried using a function to identify the row in the component header that has the "original" flag value, but that makes the whole sheet run ridiculously slowly, since it has to execute that function a thousand times any time any cell changes.

    Any other suggestions?

    Thanks,
    Aaron

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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