+ Reply to Thread
Results 1 to 13 of 13

How to get Min / Max Row / Col in a non-contiguous range?

  1. #1
    Forum Contributor
    Join Date
    10-10-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2016
    Posts
    214

    How to get Min / Max Row / Col in a non-contiguous range?

    Right now I define it by rowing through each cell:

    Please Login or Register  to view this content.
    and I achieve the results I want, but it seems to me to be a very inefficient way to do it. I don't want to row through millions of cells just to find the edges of this range!!!!

    There must be a better way? Thanks.

  2. #2
    Forum Contributor
    Join Date
    10-10-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2016
    Posts
    214

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    I've also tried:
    Please Login or Register  to view this content.
    but this only works if the user selects continuous rows/columns

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    If you want the highest row number, loop through the Areas in the selection and check the row number of the last cell in each one, storing the max value.
    Remember what the dormouse said
    Feed your head

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    To extend Rory's answer a bit: try the following code in an empty sheet:

    Please Login or Register  to view this content.
    And now you can drop all the displaying-related parts of the code and use it for your somerange
    Best Regards,

    Kaper

  5. #5
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,009

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    Just another way:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by superlative View Post
    There must be a better way?
    Mayve via the Range.Find method …

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    Hi Marc,

    I think that Range.Find will look on whole (potentially not continous) range and locate specihic values (What:=) so I'm not sure how it could be used to find "smalles surrounding rectangle" address - I think this is what superlative looks for.

    PS. My code from above with removed side/demonstration parts seems to be still competitive compared to range.address and address splitting method:

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    I was thinking for cells containing data instead of an easy last column # from a range for example …

  9. #9
    Forum Contributor
    Join Date
    10-10-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2016
    Posts
    214

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    Hi everyone,

    thanks for all the great ideas!! I actually managed to find some code online very similar to kaper's and modified it according to my needs. Here it is:
    Please Login or Register  to view this content.
    using WorksheetFunction.Min appears cleaner, but is there a performance benefit? Anyone know? Thanks!
    Last edited by superlative; 11-06-2019 at 01:20 PM.

  10. #10
    Forum Contributor
    Join Date
    10-10-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2016
    Posts
    214

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    Akuini: do you think your code is faster? I use this function extensively so I need it to be fast. Thanks!

  11. #11
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,009

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    Quote Originally Posted by superlative View Post
    Akuini: do you think your code is faster? I use this function extensively so I need it to be fast. Thanks!
    Well, you need to test it, you can try putting the codes in say 10K loops then see how long it takes, I'm guessing mine is a bit faster but I think the difference won't be noticeable in practice.
    I changed my code a bit & put it in 10K loops & used a range with more areas, it took 1.28125 seconds.

    Please Login or Register  to view this content.
    The result:
    rows: 3:22
    columns: 2:10
    1.28125

    Test your code by using the same set up, see how it goes.

  12. #12
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,009

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    I forgot something, using range address has a limit, the string address can't be longer than 255 characters, otherwise it will raise an error.
    So I think your code or Kaper's code is better.

    But actually how do you get the range? By selecting the range manually?
    Last edited by Akuini; 11-06-2019 at 09:03 PM.

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: How to get Min / Max Row / Col in a non-contiguous range?

    Quote Originally Posted by superlative View Post
    using WorksheetFunction.Min appears cleaner, but is there a performance benefit? Anyone know? Thanks!
    I would expect a call to Worksheetfunction to be slower, but you should test it.

+ 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. non-contiguous named range - how to use in formulas, or how to convert to contiguous list
    By david killoran in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-23-2019, 03:33 AM
  2. [SOLVED] Copy range and paste into non contiguous range
    By amros in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 07-02-2017, 05:42 AM
  3. excel vba range property non contiguous range
    By Ramo13541 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2016, 08:23 PM
  4. [SOLVED] Using scripting dictionary to add contiguous group of items from non contiguous range
    By stnkynts in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2014, 06:12 PM
  5. [SOLVED] How to delete multiple contiguous (and/or) non-contiguous rows, in an excel table
    By jimmalk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-03-2012, 05:48 AM
  6. Contiguous "Range" from Non Contiguous Ranges
    By DonkeyOte in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-03-2011, 03:23 PM
  7. Copy and Paste an array (contiguous & non contiguous ranges)
    By Xrull in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2010, 09:17 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