+ Reply to Thread
Results 1 to 14 of 14

Finding Max Value with a Loop

  1. #1
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Question Finding Max Value with a Loop

    Hi all,

    I need a VBA code to find first 5 max values in the matrix. The process lasts as follows:

    1-Find the max value
    2-Write that max value in the second sheet on A1 cell
    3-Erase that max value in the matrix
    4-Find the max value
    5-Write that max value in the second sheet on A2 cell

    and go on...

    I tried to code but I encounter error messages always.

    My code for finding the first max value:

    Please Login or Register  to view this content.

    Thanks for helping
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Finding Max Value with a Loop

    Please Login or Register  to view this content.
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Finding Max Value with a Loop

    Hi, Oxco,

    any reason why you wonīt use the worksheetfunction Large for that? OK, Large that wonīt delete the Max but for using the function thereīs no need to delete the max.

    Place this formula into A1 on the results sheet and adjust the name of the data sheet as well as the range
    Please Login or Register  to view this content.
    Then drag down as needed.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Finding Max Value with a Loop

    Thanks patel45, it works well but I also have to need erased cell value in that matrix.

    any reason why you wonīt use the worksheetfunction Large for that? OK, Large that wonīt delete the Max but for using the function thereīs no need to delete the max.

    Place this formula into A1 on the results sheet and adjust the name of the data sheet as well as the range
    Yes, you're right about that HaHoBe, it is easier to find and sort the values with a formula. However, I have a complex algorithm actually and this is the first step of my algorithm. Because of this reason, I can not use a formula to reach what I wanted. Thanks anyway

    You can examine http://www.excelforum.com/excel-prog...algorithm.html

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Finding Max Value with a Loop

    Hi, Oxco,

    and not being able to use a formula makes using the worksheetfunction obsolete? Please change worksheet names and range as needed.
    Please Login or Register  to view this content.
    No need for loops here.

    Ciao,
    Holger

  6. #6
    Forum Contributor
    Join Date
    07-04-2012
    Location
    Al Khor, Qatar
    MS-Off Ver
    Excel 2013
    Posts
    101

    Re: Finding Max Value with a Loop

    Hi oxco,

    Please check this code,

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Finding Max Value with a Loop

    Thanks for both.

    Holger, it works well. I have still need to cell to be erased because sorting is not my goal. Actually, I will choose the max. value according to the results of if function. Thanks again.

    Tangangtanga, the code you write does not find the second max value.
    Your results as follows;

    121561
    1891981
    5841651
    113213213
    187630463
    198790549
    339608409
    373368945
    391739447
    566003605
    584688345
    792398801
    1018793997
    1245189193
    1471584389
    1697979585
    1924374781
    32123184731


    However I want to first 5 max value. The first max. value found and written in the second sheet and then the second max. value should be found after deleting the first max. value. You can observe the attached file.

    Thanks,
    Oxco
    Attached Files Attached Files

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Finding Max Value with a Loop

    Hi, Oxco,

    from what I understand by now you could narrow the problem down a bit. If you need them you can still write the numbers to any place you want. At the same time you could search for these values in the sheet and delete that number in order to search for the Max again (only a slight change in the formula to be evaluated). If you use Find and set a range object to the cell if itīs found you can delete the value with ease. The following code goes to work after the numbers have been placed:

    Please Login or Register  to view this content.
    Okay, we use a loop but only to look for multiple values one after another.

    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Finding Max Value with a Loop

    Yes, I try to narrow the problem

    Thanks for helping but I could not run the code. Nothing is changed in my workbook.

    May be I should tell what I actually need. You can observe in the attached file.

    Thanks
    Attached Files Attached Files

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Finding Max Value with a Loop

    Hi, Oxco,

    youīre right with

    I could not run the code. Nothing is changed in my workbook.
    I canīt run any code in the workbook myself (there is none?).

    So what code did you use for which purpose?

    Ciao,
    Holger

  11. #11
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Finding Max Value with a Loop

    Hi Holger,

    I meant I could not run the code you post it;

    Please Login or Register  to view this content.

    I have no experience and knowledge about any coding language. The example file is a small version of my project but I can not progress. And that's the situation.

    Thanks for your interest.

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Finding Max Value with a Loop

    Hi, Oxco,

    the posted code would not have any effect on your sheets as the worksheets mentioned do not exist in the sample.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  13. #13
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Finding Max Value with a Loop

    Thanks, Holger.

    Now, I must use your code to find the max. value according to the result of "if" object.

  14. #14
    Registered User
    Join Date
    04-22-2013
    Location
    Ankara
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Finding Max Value with a Loop

    Thanks, Holger.

    Now, I must use your code to find the max. value according to the result of "if" object.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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