+ Reply to Thread
Results 1 to 5 of 5

The smallest value in column based on criteria

  1. #1
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    The smallest value in column based on criteria

    I got a code that is working OK at the moment. The problem is that this code is super slow. I was wondering if there might be a more efficient way to solve this problem.
    I got two different workbooks (wb1 and wb2) with one sheet each (ws1 and ws2). The first workbook (wb1) which is my main workbook is about 7000 rows with 22 columns.
    My second workbook (wb2) is instead 350000+ rows with 28 columns.

    What do I want to do with my code? In my main document (wb1) then I want to get the lowest value in wb2 based on a criteria in column 5 (wb1). In other terms, its like =min(if(..)) in a excel array formula. Is there anyway that I can make this more efficient?

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: The smallest value in column based on criteria

    Hi Gandreso

    Try this:
    Please Login or Register  to view this content.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    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,864

    Re: The smallest value in column based on criteria

    So basically, you look for a minimum value in column 15 for each value in column 5 (both activesheet)
    May be it will be quicker to start with sorting activesheet by two keys - parameter as primary key (column 5) and values (column 15) (order does not matter - may be ascending) as second key - value - column 5 (here obviously ascending orger.
    now if you go from the top the value (column 5) for a first occurence of given parameter (column 15) is the minimum, and you could use it for filling column 18 in wb1.Sheets(ws1)
    It could be even easier if output sheet is also sorted (one key here - just parameter - column 5 - sorted the same order as in activesheet)
    Sort is a very quick and effective method and your dataset is quite big. so it may be a berrer idea than dictionary object.

    May be the text above is not super-clear, buy for sure it would be easier to show it in the macro working on a dummy data in attachment than just to write anb describe
    Best Regards,

    Kaper

  4. #4
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Re: The smallest value in column based on criteria

    Thanks for your answer sintek!

    The screenupdating part is already build into my code, but forgot to add those, but thanks anyway.
    Going with elseif instead of else and than if is a upgrade, so thank you. But my code is still too slow, since its so much looping.

    Quote Originally Posted by sintek View Post
    Hi Gandreso

    Try this:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Göteborg
    MS-Off Ver
    2010
    Posts
    130

    Re: The smallest value in column based on criteria

    To make it clear what I want to do;

    1. Column 5 in wb1 is my criteria, might look like this 5181833.
    2. Find the lowest value in column 15 in wb2 that has my criteria (5181833) in column 5.
    3. Add the lowest value in column 18 in wb1, lowest value might look like this 201716.
    4. Go next row (criteria) in wb1

    I like your idea, its smart! Im gonna try it out, it might be a lot better. I will get back to you with some results.

    Quote Originally Posted by Kaper View Post
    So basically, you look for a minimum value in column 15 for each value in column 5 (both activesheet)
    May be it will be quicker to start with sorting activesheet by two keys - parameter as primary key (column 5) and values (column 15) (order does not matter - may be ascending) as second key - value - column 5 (here obviously ascending orger.
    now if you go from the top the value (column 5) for a first occurence of given parameter (column 15) is the minimum, and you could use it for filling column 18 in wb1.Sheets(ws1)
    It could be even easier if output sheet is also sorted (one key here - just parameter - column 5 - sorted the same order as in activesheet)
    Sort is a very quick and effective method and your dataset is quite big. so it may be a berrer idea than dictionary object.

    May be the text above is not super-clear, buy for sure it would be easier to show it in the macro working on a dummy data in attachment than just to write anb describe

+ 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. Smallest values in a column excluding zeros and also an extra criteria
    By akash.ksa in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-22-2016, 10:03 AM
  2. [SOLVED] Find Smallest Number in Column And Set Criteria
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-04-2016, 05:44 PM
  3. Smallest Number based on criteria from several columns
    By qwertyyy in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-21-2015, 11:32 PM
  4. [SOLVED] Return column title based on smallest value by row?
    By realrookie in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-29-2015, 12:58 PM
  5. [SOLVED] Smallest/largest values based on two criteria
    By jcswaby in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-19-2015, 06:41 AM
  6. Replies: 2
    Last Post: 02-02-2015, 10:19 PM
  7. Replies: 5
    Last Post: 09-29-2011, 06:39 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