+ Reply to Thread
Results 1 to 20 of 20

Quickest way to iterate through a range

  1. #1
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Quickest way to iterate through a range

    I have a matrix with 58 columns and 766 rows, so 44428 cells in total. This matrix is populated with forecasts/statuses for 'requirements'. The status could be "In Stock", "No Order", a date "12/11/17" or something else. The cells are also colour coded and formatted. I use a series of checkboxes on a userform to toggle whether or not I want these cells to be displayed. The following code then iterates through every cell in the range and tests it based on cell format and adds matching cells to a variable range before displaying the entire row/column of the matching range.

    Now this works fine with only one or two criteria ticked, taking a second or two but with more boxes ticked (there are eight boxes) it can take up to 10 seconds to refresh. This is a live application so I need it to be more efficient so I'm looking for a more elegant solution. I realise here I am testing each cell up to eight times so maybe I can do something to dynamically change the range as it iterates.

    Another thing I thought was to assign variables to the colours and then add those to an array so it only has to iterate once regardless of the number of boxes checked but this is just slightly beyond my level of understanding

    Any suggestions? Thanks in anticipation

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    make it the other way around - loop once over dRng and then do checks.
    Also, with small amendments the code could be reduced significantly
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Quote Originally Posted by buran View Post
    make it the other way around - loop once over dRng and then do checks.
    Could you please expand on this? I don't understand, sorry

    Quote Originally Posted by buran View Post
    Also, with small amendments the code could be reduced significantly
    Again, could you please point me in the right direction. I know it's not the most elegant solution, I just need a few pointers.

    Thanks for looking

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    Something like this

    Please Login or Register  to view this content.
    Note you need to rename checkboxes:
    1. The name of the checkboxes that take care of cells, based on interior color should start with cbx_ic. Also color code as Tag property. If more than one color (checkbox9) - separated by comma
    2. The name of checkbox that takes care based on font style should start with cbx_fs

    Of course you can change this the way you want


    Note I'm not able to test the code, so there might be errors
    Last edited by buran; 09-20-2017 at 04:37 AM. Reason: Add one more if

  5. #5
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Hi,

    Thanks for the solution Buran. I'm afraid it's not working although your method looks sound. I think it's an easy fix but I've not been able to figure it out. When I step through the code ctrl = Nothing so there appears to be an issue with the loop through the userform controls. Maybe the userform isn't being initialised or something?

    The following test code workd fine though.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    Can you upload sample workbook? I assume the code goes in the userForm1 class module?

  7. #7
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Ok I found the bug
    Please Login or Register  to view this content.
    should have been

    Please Login or Register  to view this content.
    The code now executes however isn't particularly fast and also returns unexpected results, i.e. selections that should return results show none.

    This gives me a good start and an alternative approach so thank you for your help so far Buran

  8. #8
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    Should be

    Please Login or Register  to view this content.
    This one should work

  9. #9
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Thanks

    Ok so now I know why it's returning unespected results. Only the cbx_fs checkbox is being included.

    Additionally, it seems that the current version may be slow because for each c in dRng the userform controls are being looped. This only needs to be done once to set the my_colors variable. If we remove this from the outer loop it should be much quicker.

  10. #10
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    no, my_colors cannot be populated only once. You need to populate Tag property for each cbx_ic checkbox control with the respective color code e.g. for current Checkbox8 you need to set Tag property to 10921638
    However, because Checkbox9 has to take care of 2 color codes - 5287936 and 5296274 I use split and my_colors. For this checkbox you need to provide Tag property as 5287936,5296274. If every checkbox has only one color code you can use directly Tag property without the need to split and iterate over my_colors.
    Setting tag property can be done at design time.
    Note that my code runs over all cells in dRng once while yours iterates as many times as check controls there are.
    If you want to iterate only over CheckBoxes then put them in a Frame control and after that iterate only over controls in that frame. This will make difference if you have many more controls on the form

  11. #11
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Hi,

    I had already added the ctrl tags so that was not the problem. I've also added another checkbox and separated the two colours from checkbox9 to remove that complexity.

    Where I'm stuck at the moment is I can't see anywhere in your code that the ctrl.tag is added to the variable my_colors so when the loop is iterating through each c in dRng the variable is not set so no matching cells are found. The only cells found are those with bold font.

    So I have researched variable arrays and made the below changes. Firstly, I iterate through the checkboxes and add all selected tags to the array. I've checked this in the immediate window and the variable my_color() reterns the relevant variable. I have separated this from the dRng loop so it does not need to reset the variables for every loop.

    The next part is where it falls down. For each c in dRng it should check the c.interior.color against each color variable in my_colors. It does this but never finds a match. I believe this is because the my_color() variable taken from ctrl.tag is a string and the c.interior.color is a colorformat.

    Any ideas? Am I looking at this in the wrong way?


    Please Login or Register  to view this content.

  12. #12
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    In my code I have
    Please Login or Register  to view this content.
    this line splits ctrl.Tag of the CURRENT ctrl and makes one-dimensional array. Then I loop over it
    Please Login or Register  to view this content.
    which was only because of the checkbox with 2 color codes.
    i.e. ctrl is former CheckBox8 it will have one item in the array, ig ctrl is former CheckBox9 it will have only two, etc.

    now, when you have only one color code per checkbox then you can ammend the code as follows

    This part
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    are you sure you have renamed the checkboxes correctly?

  13. #13
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    again, can you upload a workbook. it will be much easier

  14. #14
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    OK, i've got it to work now. The my_color variable type was wrong. It should have been "double". So the code now works, although isn't particularly fast. Similar in fact to the previous code, approximately 10 seconds if all checkboxes are ticked

  15. #15
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Quote Originally Posted by buran View Post
    again, can you upload a workbook. it will be much easier
    It is full of sensitive data, I will have to cleanse it. Please bear with me.

  16. #16
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    remove the data, I would prefer to see the code and the form

  17. #17
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Hi,

    I've attached a cleansed workbook.

    Please can you take a look? I want to set the variables based on the checkboxes then iterate through the cells and if they match the variable values then display those cells.

    There are lots of other controls in the userform too.

    Many thanks
    Attached Files Attached Files

  18. #18
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    OK, thanks for the workbook. I had a look at it. There is some code I'm not sure I fully understand why you need it, but anyway I see you follow the general approach I suggested. I see what you mean by populating my_colors array in advance. The only other thing that comes to my mind and could speed it/reduce the number of cells you iterate over is to iterate row by row of dRng and once there is cell that will be visible (meaning the entire row will be visible too) to skip the rest of the cells in this row and continue with the next row.

  19. #19
    Forum Contributor
    Join Date
    10-25-2011
    Location
    Preston, England
    MS-Off Ver
    Excel 2016
    Posts
    136

    Re: Quickest way to iterate through a range

    Hey Buran,

    Quote Originally Posted by buran View Post
    There is some code I'm not sure I fully understand why you need it
    Which is the code you're referring to? This is a cleansed development version so there is some test code in there which I was using for debug purposes. There is some code which retains and reapplies the filters. Now I look at it, I'm not sure I need it anymore.

    The report is a logistics overview of parts required for manufacture of major units. The first 20 are now complete so there is some time wasted iterating through these cells. One way I plan to speed it up is by doing a precleanse of the dRng with something like for each column in dRng, if(countif (dRng,"Clear")=dRng.rows.count then .entirecolumn.hidden=true. Then set dRng to drng.specialcells(xlcelltypevisible) before calling the main macro.

    I like your idea of iterating row by row and I will have a think about how to implement it.

    One final edit I have also made since I uploaded the file yesterday is below. Adding an Exit For into the colour check loop as beforehand it was continuing to check all colors in the array even if it had found a match.

    Please Login or Register  to view this content.

  20. #20
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Quickest way to iterate through a range

    Quote Originally Posted by radddogg View Post
    Which is the code you're referring to? This is a cleansed development version so there is some test code in there which I was using for debug purposes. There is some code which retains and reapplies the filters. Now I look at it, I'm not sure I need it anymore.
    Yes, exactly the code dealing with filters. I didn't put much effort to look into them in more detail

+ 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] Apply formula to large range the quickest way
    By BG1983 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-29-2016, 01:44 PM
  2. [SOLVED] Quickest Way To Fill A Range In Ascending Order
    By Philb1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2013, 06:12 PM
  3. to iterate within selected range and loop
    By dazzwilliams in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-22-2011, 01:27 AM
  4. Iterate through a Range of selected Cells?
    By TonyHine in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-04-2009, 08:06 PM
  5. Iterate through merged range
    By MrFoxar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2009, 06:38 PM
  6. need to erase range (quickest way)
    By supermario77 in forum Excel General
    Replies: 1
    Last Post: 04-26-2005, 07:06 PM
  7. How to iterate over columns in a range
    By rgans in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2005, 06:13 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