+ Reply to Thread
Results 1 to 19 of 19

Top 3 formula or macro

  1. #1
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Top 3 formula or macro

    I have a spreadsheet with categories in columns and samples in rows. I need to get the top three categories for each sample. It's quite similar to the question asked in this recent thread asking for the top 5 items, and the answer given there, =INDEX(A:A,MATCH(LARGE(U:U,ROW()-1),U:U,0)), almost works for me with a little modification, except that it fails when there are duplicate values (it returns the first one twice). My values range between 0 and 1, and in many cases, there will be two cells with a value of 1. It seems I need a VBA program or an array function to do this for me, but I only sort of understand how to use array functions and I know virtually nothing about how to use VBA.

    Can anyone help?
    Last edited by drfarmkid; 12-13-2011 at 12:32 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Top 3 formula or macro

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbook. Manually demonstrate the results you want and point them out.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    Here's an example. I didn't attach the whole workbook because it's quite large (over 1000 columns). What I did attach, though, shows what I'm looking for. The columns I want for each row are highlighted in yellow. The differences between what I'm currently getting and what I want it to return are in red.

    Thanks for taking a look.

    Attachment 131766

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Top 3 formula or macro

    It appears the forum is broken. Can you upload the file online somewhere and then put a link here?

    Or you can send me the file through the link in my signature.

  5. #5
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    I'll try attaching it again. My attachment looks different than that of others who have uploaded attachments. I'm new to this forum, so it's entirely possible, likely even, that I did it wrong. If it still doesn't work, I'll email it to you or find another way to link to it.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    05-24-2011
    Location
    India
    MS-Off Ver
    365
    Posts
    2,243

    Re: Top 3 formula or macro

    Try this,

    AO3,

    =INDEX(A$1:AM$1,,MATCH(LARGE(A3:AM3,1),A3:AM3,0))

    AP3,

    =INDEX(A$1:AM$1,,MATCH(LARGE(A3:AM3,1+COUNTIF(A3:AM3,MAX(A3:AM3))),A3:AM3,0))

    AQ3,

    =INDEX(A$1:AM$1,,MATCH(LARGE(A3:AM3,1+COUNTIF(A3:AM3,">="&SMALL(A3:AM3,FREQUENCY(A3:AM3,MAX(A3:AM3)-0.00000000001)))),A3:AM3,0))

    copy down.
    Regards,
    Haseeb Avarakkan

    __________________________________
    "Feedback is the breakfast of champions"

  7. #7
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    That almost works, except that it eliminates redundant values, rather than returning all of them. I need the categories of the three highest values, even if they are redundant. Because of what the data represents, in an ideal situation, almost every row would have two values of 1.0, then several with lower values. Using LARGE would work great if there weren't redundant values, but since there are, all I get is a duplicate of the first one of the two redundant values and none of the second because each cell does the calculation anew and returns the first value that satisfies the criteria. I guess I need some way to ignore the first value that satisfies the criteria and return the second if there is one (for the second column, and likewise for the third), or an array function or VBA program that can generate and return all the desired values simultaneously.

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Top 3 formula or macro

    It is not clear from your example to see what you are after.

    Why is there a result in Cell AV6?

    What is to happen with tied results?
    i.e. You could have more than 3 "tied scores" in any of the first 3 positions.

    This workbook returns the first 5 found results, does this help?

    Unhide the helper columns with the grouping button.
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  9. #9
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    Yes, that works. Thank you. I didn't explain cell AV6; I guess I just assumed it would make sense. In that case, there are duplicate values for the third highest value, and I want both of them.

    I was about to ask what "ReturnOutcome" is, but then I found it is a VBA object you (or someone else?) wrote. It works great in the example file, but not quite when I copied it to my actual spreadsheet. I copied the appropriate cells to the appropriate place and changed the references appropriately, but it didn't work. Then I found I had to copy the VB module to my workbook, after which the error changed from a #NAME error to a #VALUE error. Now it gets interesting: I have calculation set to "manual" because it's a large complicated spreadsheet that takes several seconds to calculate so naturally I don't want it to do that every time I make any change. If I hit the "calculate now" button in the example workbook you sent me, it calculates both sheets and it works, but if I click that button or the "calculate sheet" but in my workbook with the actual data, it returns a #VALUE error. I tried just copying everything from the sheet you sent me to a new workbook, copied the module to the new workbook, and that also produces the error. It seems I'm missing something here. What is the proper way to implement what you sent me into my workbook?

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Top 3 formula or macro

    A good start would be a sample of your actual workbook.

    The example you provided has leading/trailing spaces etc., and seems to have a different layout from your actual workbook.

    The VBa in the example I posted is "hard coded" to your posted sample.

    Post what you have tried up 'till now.

  11. #11
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    What I posted was a sample of my actual workbook. I just copied and pasted some of the columns with the data I need help with because the whole thing is rather large and complex. If you think it will help, I'll go ahead and post the actual workbook.* The columns I'm trying to get the information from are columns AJC through AML.

    It seems to me that I'm doing something wrong where it comes to copying the module in the workbook you sent me. The reason I think that is because, as a test, I created a new workbook and just copied and pasted everything from the example file into the new one. I then copied the module to the new workbook. (Ctrl+A, Ctrl+C, Ctrl+N, Ctrl+V, go into VB editor, drag module from drfarmkid_example.xls to Book1.xls, Save As test.xls) This new one should have been exactly the same as the example you sent me. That one didn't work either, and in fact that one always produces the #VALUE error, regardless of which workbook I press the "calculate now" button in.

    Thanks again for your help.

    *When I tried to upload the workbook, I received an error that the file was too large (~2.6MB). I deleted all but 5 of the rows; that should be sufficient.
    Attached Files Attached Files

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Top 3 formula or macro

    The problem is with the layout of your workbook compared to your sample.

    The sample range is Columns A:AM and the VBa is set to this, your actual range is AJC:AML. (See the named range "Headers")

    It's late here and I only have 2003 available at the moment, I'll need 2007 to modify, and check, this for you. Look back tomorrow.

    Is AJC:AML constant?
    Last edited by Marcol; 12-08-2011 at 08:08 PM.

  13. #13
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    No, that range is not constant. I may expand or contract or move as I process more data.

    I wondered about the range issue, but I wasn't sure. I don't know VBA at all, and have only a basic understanding of programming and not enough working knowledge of any language to do anything useful. I tried to follow your code, and I more-or-less understand it, but I wasn't sure what "Sheets("Sheet1").Range("Headers")" meant. I thought that might be the problem, and I changed "Sheet1" to match the name of the sheet in my workbook with no effect, and I thought "Headers" might refer to a named range, but I couldn't find anywhere that it was defined so I wasn't sure.

    After reading you last post, I tried defining AJC:AML as "Headers", and changing "Headers" in the code to "AJC:AML" and both worked. It will work to do that, as long as I remember to go change that any time the range changes. It may also make it harder to give the spreadsheet to colleagues to use. Is there a way to make it automatic?

  14. #14
    Forum Guru
    Join Date
    05-24-2011
    Location
    India
    MS-Off Ver
    365
    Posts
    2,243

    Re: Top 3 formula or macro

    drfarmkid,

    I am still not sure is this you require. On a separate sheet created a drop down list. You can see the Top3 values & it's correspondent values below. The data is dynamic.. When you enter/delete entries will update.

    See the attached.
    Attached Files Attached Files

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Top 3 formula or macro

    I think if we are to automate this workbook process, we should take a few steps back.

    1/. With Sheet "MHC-I data"
    Create a dymamic named range to return the "Used Range" of the table, no matter the size of what is pasted.
    Name:= MCI_I
    Refers to:=
    Please Login or Register  to view this content.
    2/. With Sheet "MHC-I haplotypes"
    Best we get rid of the volatile function OFFSET()
    Try this in C3 (after you have created the above named range)
    Please Login or Register  to view this content.
    Drag Across and down as required.
    (This will be slower to load than your formula but once loaded it will not recalculate every time you make changes in the workbook.)
    Does that match your anticipated result?

    3/. How are Rows 1 & 2 up to Column AJA in Sheet "MHC-I haplotypes" decided? Is this a manual operation?

    4/. How is Row 2 from Column AJC onwards in Sheet "MHC-I haplotypes" decided? Again, is this a manual operation? (I suspect you would like it automated)

    If you can confirm that this works, and answer the above questions then we can take the next step forward.

    Cheers
    Alistair

  16. #16
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    1.
    Okay, done. I do have a question about this, though, that perhaps you can help me understand. I wasn't familiar with dynamic ranges and didn't know how to create them until reading your post. After a quick Google search, I learned what I needed to know, and the example I read used an OFFSET function to create it. Following that example, I tried creating a dynamic range using the formula
    Please Login or Register  to view this content.
    and that seemed to work just as well. It's a simpler formula that I can actually understand, whereas I can sort of follow yours but the nuances escape me. Perhaps you can help me understand why you did it the way you did.

    2.
    Done, that works fine as well.

    3.
    Rows 1 and 2 are indeed generated manually based on data from past experiments. Just a little background: these data come from large numbers of DNA sequences. They essentially represent versions of genes that appear together in certain defined groups. They will change from time to time as we discover new groups and new sequences and/or find that the current groups/sequences are incomplete or incorrect.

    4.
    That list comes from Row 1. They are the same, except that the blank cells have been removed. I generated that by copying/transposing Row 1 into another spreadsheet, removing the blank rows with a filter, then copying/transposing back into its current location. It's important that it's in the same order as Row 1, since the formulas in the rows below reference those to find the appropriate locations in the main block of data. I had planned to just redo that whenever the changes mentioned in #3 above necessitated an update, but if there's a simple way to automate that I certainly wouldn't complain.

  17. #17
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Top 3 formula or macro

    1/. With the potential size of your data matrix you should seriously avoid volatile functions such as OFFSET().
    INDEX("your range",MATCH(),MATCH() ) is much more efficient.

    4/. Okay, I think I'm with you when combined with your reply to "3/,", this can easily be produced automaticaly.

    To much beer tonight ... lookback in the morning ... ...

  18. #18
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Top 3 formula or macro

    Okay, have a look at this workbook.

    With the potential size of your data matrix, I think it will be better to use seperate sheets for each calculation.

    1/. "MHC-I data" is your original data sheet
    2/. "MHC-I haplotypes" is your manually created data sheet, all calculations are based on this sheet.
    3/. "MHC-I averages" is your formula transfered from "MHC-I haplotypes" (This can be hidden)
    4/. "MHC-I Rankings" returns the result you are looking for.
    Select from the drop-down in N2 to obtain different ranking lists

    Because I am limited to 2003 at the moment I had to chop your workbook a fair bit but you should be able to increase the ranges to cover the largest matrix you expect to use by dragging the various formula.

    You might also want to use IFERROR() to shorten some of the 2003 formulae, and cover any returned errors I have been to lazy to do.
    Similarly you could extend the C/F colours, 2003 is limited to 3

    Looks like there is a problem with the Advanced reply editor, so I will answer any queries you have once you get this.

    The Attachment facility seems to be pregnant again ...

    Try downloading from my Skydrive
    example of workbook.xls
    Last edited by Marcol; 12-12-2011 at 08:08 PM. Reason: typos

  19. #19
    Registered User
    Join Date
    12-07-2011
    Location
    Utah
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Top 3 formula or macro

    Now that I'm back at work, I've taken a look at what you did. I like it. It's certainly set up better than what I had, and I think it will work better. I really appreciate all your help. I've also learned several tricks and approaches that should be useful in the future as well.

+ 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