+ Reply to Thread
Results 1 to 12 of 12

Do Until / Loop code to find max value in many adjacent columns?

  1. #1
    Registered User
    Join Date
    10-15-2010
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Do Until / Loop code to find max value in many adjacent columns?

    Hi. I have many columns of numerical data with headings starting in Column B, Row 8 (all the way to Column GI in this instance but the number of columns is not always the same).

    The range of values starts in Row 9 of each column (ie. just below the headings). Let's say Column C's data stops at Row 59, Column D's data stops at Row 74, and so on... each column length is different but never more than 5000 (hence the '5000' in the code below).

    I would like to find the maximum value in each column and have this value placed in Row 2 of each column.

    Here's my code that doesn't work...

    Please Login or Register  to view this content.
    (Note: MultiLetter is a function I have defined to convert column integer identifiers into the normal letter identifiers... ie. "3" is Column C).

    I get a VisualBasic error, Run-time error '1004': Unable to get the Max property of the WorksheetFunction class.

    Clicking the 'Debug' button takes me to the Cells(CurrentCol_I, 2) =... line.

    Any ideas as to how this line should be written? Or if there are other mistakes in the code?

    Thanks in advance.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Do Until / Loop code to find max value in many adjacent columns?

    Hi

    how about
    Please Login or Register  to view this content.
    rylo

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do Until / Loop code to find max value in many adjacent columns?

    Welcome to the forum.

    One way:
    Please Login or Register  to view this content.
    The code considers the fact that the used range does not always include A1.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Do Until / Loop code to find max value in many adjacent columns?

    If there are no numeric values above row 9 then you could also use entire column references in the MAX.
    The MAX function will only use the Intersect of the Used Range and the precedent range (unlike SUMPRODUCT / Arrays).

    edit: just to be clear - I'm not saying you *should* ... the above is more to do with how MAX works (in terms of worrying about last row etc...)
    Last edited by DonkeyOte; 10-15-2010 at 05:04 AM. Reason: typo

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do Until / Loop code to find max value in many adjacent columns?

    If there are no numeric values above row 9 then you could also use entire column references in the MAX.
    My thought was that there might be values in row 2 from a previous invocation.

    But on that topic, DO, do you have a list (or reference to) the functions that clamp to the used range?

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

    Re: Do Until / Loop code to find max value in many adjacent columns?

    Quote Originally Posted by shg
    DO, do you have a list (or reference to) the functions that clamp to the used range?
    Unfortunately not... my belief is that this holds true for the majority of functions with the exception of SUMPRODUCT & Arrays and things like LOOKUP(2,1/...) & INDEX(...,0) etc...

    I've seen Charles Williams demo/test by comparing the calc. time of a given function using entire column & specific range precedents - and showing results to be the same (using his own precision tools of course).

    This is one of the reasons I'm wary of advocating DNR's when specifically used to restrict range references (I know you like them for other reasons like visibility).
    A lot of functions are so efficient the overhead in establishing the "used range" is not always necessary.
    Last edited by DonkeyOte; 10-15-2010 at 11:26 AM. Reason: missing word

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do Until / Loop code to find max value in many adjacent columns?

    DNR = Do Not Resuscitate, as in, Can this formula be saved?

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

    Re: Do Until / Loop code to find max value in many adjacent columns?

    to be specific... Dynamic Named Ranges

    there was a great (and I mean great) exchange a while back at MrExcel involving Charles W:

    http://www.mrexcel.com/forum/showthread.php?t=371570

    (do I get infracted for posting that link ?)

    edit:

    Quote Originally Posted by shg
    My thought was that there might be values in row 2 from a previous invocation.
    D'oh !
    Last edited by DonkeyOte; 10-15-2010 at 11:45 AM.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Do Until / Loop code to find max value in many adjacent columns?

    Thanks for the linkm, that was interesting. It did not surprise me that Excel has no concept of 'used column' vs 'used range;' there's nothing in the object model to suggest otherwise.

    (do I get infracted for posting that link ?)
    I posted a link yesterday from there to here for an OP who wanted to post a file for me to look at. So far, I haven't been shot ...

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

    Re: Do Until / Loop code to find max value in many adjacent columns?

    I took the liberty of emailing Charles Williams regards "the function list" and he has kindly replied as follows:

    Quote Originally Posted by Charles Williams
    I don't have a list -

    In theory its only functions that need a built-in iterator like SUMPRODUCT and Array formulae that have a problem, but of course there may well be some long-standing bugs where other functions are not correctly programmed.
    INDEX, VLOOKUP, SUMIFS are OK (although some of the weirder uses of INDEX might not be), don't know about LOOKUP.
    UDFs need to be explicitly programmed.

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Do Until / Loop code to find max value in many adjacent columns?

    Please Login or Register  to view this content.
    And if you want to take the usedrange per column into account you could use a UDF:

    in A2: =max(snb(9,column()))

    Please Login or Register  to view this content.
    Last edited by snb; 10-16-2010 at 03:35 PM.



  12. #12
    Registered User
    Join Date
    10-15-2010
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Do Until / Loop code to find max value in many adjacent columns?

    (Apologies for not coming back to this sooner...)

    Thanks for all the replies and suggestions. Lots of good stuff and I've learnt much from it.

    Here's what I tried (which works):

    Please Login or Register  to view this content.
    Cheers.

+ 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