+ Reply to Thread
Results 1 to 11 of 11

Why is this code slow?

  1. #1
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Why is this code slow?

    For some reason my cursor takes more time to move from cell to cell than I think it should. It is a noticeable time delay.
    I have narrowed it down to the following code.
    It runs from every Worksheet_SelectionChange().
    Please Login or Register  to view this content.
    I only notice the slowdown on one worksheet. I'm guessing that it has something to do with my Conditional Formatting. I have lots of cells that change their cell color when the cursor is on their row.

    Is there a faster way to accomplish this?
    Last edited by foxguy; 05-16-2010 at 02:06 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,483

    Re: Why is this code slow?

    What is supposed to happen?

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Why is this code slow?

    It just stores the current location in 2 cells, so that conditional formatting can change formatting based on where the cursor is.
    It also copies the last cursor location to 2 different cells, because some subs() check to see where the cursor came from to determine different action.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Why is this code slow?

    Have you tried toggling Calculation ?

    Conditional Formatting is (super) Volatile.

    Please Login or Register  to view this content.
    It's not clear how this routine is being invoked and whether or not the above is already taking place c/o the "parent" routine(s).

  5. #5
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Why is this code slow?

    DonkeyOte;

    Toggling Calculation helped a bit, but not as much as I would like. I failed to mention that I already had EnableEvents & ScreenUpdating turned off.

    Any other ideas?

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Why is this code slow?

    Quote Originally Posted by foxguy
    It just stores the current location in 2 cells, so that conditional formatting can change formatting based on where the cursor is.
    If you're using the SelectionChange Event why the need for Conditional Formatting rules based on active cell ?

    Would it not be better to use the VBA event to format as required and thus remove the inherent Volatility ?

    Perhaps post a sample.

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Why is this code slow?

    DonkeyOte;

    I don't like putting things in the VBA that Excel can do. I read somewhere that VBA is slower than Excel.
    Plus there are so many cells that have 3 Conditional Formatting formulas, and the condition that is dependent on the current selection is not always the 1st condition. It would also get very confusing putting all those range names in my worksheets, because I refuse to put addresses in VBA, only range names

    I just thought of something. Do you think this would be faster?
    Currently one of my Conditional Formatting formulas is "=Row()=Selection_Row". If I were to instead put a formula in some hidden column Z1 : =Row()=Selection_Row, then my Conditional Formatting formula would be "=$Z1".
    My thinking is that it takes Excel longer to calculate "=Row()=Selection_Row" than it takes to calculate "=$Z1", and since the longer calculation time is now inside a cell instead of a Conditional Format it won't get calculated so much. Especially since I have as many as 5 cells in the same row that change formatting based on Selection_Row.

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Why is this code slow?

    Quote Originally Posted by foxguy
    I don't like putting things in the VBA that Excel can do. I read somewhere that VBA is slower than Excel.
    In Excel everything depends upon context.
    I'd also say that if efficiency is of paramount concern then avoiding Volatility should be pretty high up in your list of "things to avoid in your model".

    UDFs are generally speaking slower than native functions, however, a single UDF might be preferable to a hideously complex Array.

    Equally, and more pertinent here perhaps, VBA is often preferable to volatile alternatives.

    In the most basic of terms consider 1000 cells that utilise the current date c/o a link to A1 in which we have: =TODAY()
    (we assume that this date must be updated on an automated basis)

    Each of those 1000 dependants is Volatile by virtue of the Volatile precedent.

    If VBA were used to insert today's date as a Constant in A1 then though this may be slower to action than =TODAY() it removes all Volatility.
    By removing the Volatility those 1000 dependants now calculate only when necessary.
    As a result the calc overhead in the model is reduced and long term performance is improved (from an end users perspective [calc delay etc...]).

    Again though I'd reiterate the earlier point that it's context, context, context. If those 1000 dependent formulae are lightweight the impact of the Volatility would be negligible - were they Arrays or some such it would be an altogether different matter.

    Quote Originally Posted by fox_guy
    Currently one of my Conditional Formatting formulas is "=Row()=Selection_Row". If I were to instead put a formula in some hidden column Z1 : =Row()=Selection_Row, then my Conditional Formatting formula would be "=$Z1".
    My thinking is that it takes Excel longer to calculate "=Row()=Selection_Row" than it takes to calculate "=$Z1", and since the longer calculation time is now inside a cell instead of a Conditional Format it won't get calculated so much. Especially since I have as many as 5 cells in the same row that change formatting based on Selection_Row.
    Avoiding repetitive calcs is always a good idea, however, again it's all context. The impact of avoiding the repetition unsurprisingly will come down to the nature of the repetition itself. If the repetition is a lightweight calc then removing it is unlikely to make a significant impact (but is good practice if nothing else)

    Remember also that Conditional Formatting formulae are processed as though they are an Array and as such are likely to perform a little slower than you might otherwise expect
    (this issue is not significant here, however, worth being aware of for future ref. I would say)

    I guess I'd sum up by maintaining the point that improving the efficiency of your CF calcs might help but I suspect the real issue is the number of the CF calcs taking place with each selection.

    If you can post a sample we might be able to help on specifics (assuming you do not resolve to your satisfaction in the meantime) - else I'm afraid it's a case of generic chit chat from me... I'm generally prejudiced against all things Volatile so am not perhaps best placed to offer advice
    Last edited by DonkeyOte; 05-15-2010 at 02:00 PM.

  9. #9
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Why is this code slow?

    I agree with everything you say. I didn't realize the CFs were calculated like arrays.

    I'm including a sample workbook with samples of my CFs. In the real workbook, there are at least 3 times as many rows and at lease a dozen more columns, although not all columns have the same CF (except #1 - it is in all columns).
    Also, the majority of the columns in my real workbook are calculations. In the sample, the columns are not.

    Any suggestions would be appreciated.
    Attached Files Attached Files

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Why is this code slow?

    I tripled the row count and increased column count to 12 and I have to say that I didn't really notice anything adverse performance wise- ie no noticeable lag.

    Are any of your CF formulae more complex than those in your sample ?

    Are the underlying values themselves generated by formulae and if so - what are they ?

    If the underlying values are formula driven what is the performance impact of your Event on a full blown values & formats only replica of this sheet ?

    Basically I am just trying to ascertain that is indeed the CF logic that is the underlying problem and not the formulae used to generate the underlying values in the first instance.
    If they themselves are Volatile then that will also have an impact - and depending upon their complexity - that impact could be significant.

    In VBA terms (replacing the CF) - it would seem to me that you realistically you need only format two cells at any given time - ie reset prior and set current. In each case the logic used will seemingly need to vary based on column. Does that sound correct to you ?

  11. #11
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Why is this code slow?

    Basically I am just trying to ascertain that is indeed the CF logic that is the underlying problem and not the formulae used to generate the underlying values in the first instance.
    If they themselves are Volatile then that will also have an impact - and depending upon their complexity - that impact could be significant.
    It does appear that you're right, it is the complexity of the underlying formulas.

    I finally got around to doing something I've been threatening to do for months. I got rid of calculations in the CFs (on this one page). Now all the CFs just point to a cell that does all the calculations. Now I don't see the time delay (or least it is not noticeable).
    This also solves another problem I have to deal with - the fact that I can't determine any dependent CFs. Now (at least on one sheet), all the CFs point to a hidden cell that is clearly labeled with what CFs are dependent on it.

    I also realized that I had a needlessly time consuming CF in practically every cell in UsedRange, the CF that had the calculation "=A?<0". Column A cannot be changed by the user, it is only changed by a Macro that is run from a button, or during SheetActivate(). I put that calculation and corresponding format changes into the macro. I didn't think to check the performance before changing all the other CFs, but I have to believe that it helped.

    Thanks for your time.

+ 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