+ Reply to Thread
Results 1 to 7 of 7

Find Specific Number - If not found find next lowest number

  1. #1
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Find Specific Number - If not found find next lowest number

    Hola! So I'm a bit stuck at the moment. I'm trying to search a column for a specific number which is set by an input box. The column is sorted from Largest > Smallest. I originally had this code to find the number:
    Please Login or Register  to view this content.
    So what I'm trying to do is find the very first cell that contains the specified number. The problem is if the number isn't found the script fails. I would like it to search for the next closest number that is smaller than the originally number. I've tried using a For/Next statement but that has to search line by line and takes a bit more time. Is there a way to do what I need that is fast? Any help is greatly appreciated!
    Last edited by mattyp; 04-14-2017 at 10:25 AM.

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find Specific Number - If not found find next lowest number

    If your search selection is a whole column
    Please Login or Register  to view this content.
    If it is not, then the row number will need to take account of first row of selection
    Please Login or Register  to view this content.
    Last edited by kev_; 04-14-2017 at 11:41 AM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Find Specific Number - If not found find next lowest number

    Hi,

    I generally prefer to use Excel standard functionality to find stuff like this and then if required read the result into VBA rather than jump through hoops getting VBA to do something which is far more efficiently done by Excel.

    In this case the array formula (2nd below) below - entered with Ctrl-Shift-Enter will give you the relevant row number in a range.

    First though I prefer to work with dynamic range names so that I don't need to worry about the size of ranges. In my example I have a list of numbers in A1:A20 and I have defined the name 'lst' as
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Now the array formula that returns the row number of the value you seek, where C1 is the value required

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    02-23-2012
    Location
    FL
    MS-Off Ver
    Excel 2007
    Posts
    55

    Re: Find Specific Number - If not found find next lowest number

    Thank you both! I've tried both ways and they both work great. Much appreciated!!

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find Specific Number - If not found find next lowest number

    @Richard Buttrey
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    elegant solution
    but
    Can you explain the principle of how the formula works
    thanks

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Find Specific Number - If not found find next lowest number

    Quote Originally Posted by kev_ View Post
    @Richard Buttrey
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    elegant solution
    but
    Can you explain the principle of how the formula works
    thanks
    Hi,

    I'll take it as read that you understand the dynamic range name 'lst'

    The
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    returns a list of numbers from 1 to the number of rows in the range called 'lst', i.e. the numbers 1,2,3.....20. These values will be used for the index number for the SMALL() function.


    When the MATCH(C3,SMALL(lst bit is added to the above and since the whole thing is an array formula the MATCH(C3 looks for the C3 value in the lst array. Since it's a match function it returns the position of C3 in the list, i.e. the row number in the lst range.

    Since the MATCH is primarily used on an ascending list and finds the nearest value in the ascending order, and since the array returned from the rest of the formula is in ascending order the MATCH returns the position of C3 in that array. However since the actual list on the sheet is in descending order we need to deduct the value the MATCH returns from the total number of values in the list. This is the COUNT(lst)-MATCH....bit

    Hence it returns the row number required. When the value is not in the list MATCH finds the next highest value and drops back one 'row'.

    The final bit +ROW(lst) always returns the value of the first row in the list and is required to adjust for those occasions when the lst does not start at row 1. i.e. it adds the number of rows between Row 1 and the start of the list.

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find Specific Number - If not found find next lowest number

    Hi Richard
    Thank you

+ 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. Replies: 1
    Last Post: 10-07-2016, 02:16 PM
  2. VBA To find nearest biggest number and lowest number
    By gan_xl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-01-2014, 05:49 AM
  3. Replies: 5
    Last Post: 02-13-2014, 05:37 PM
  4. [SOLVED] Find number and delete until tje number is not found above it
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-24-2013, 04:36 PM
  5. Find lowest number in groups then lowest overall.
    By swieduwi in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-24-2008, 01:00 PM
  6. find lowest number?
    By qscesz in forum Excel General
    Replies: 4
    Last Post: 01-22-2008, 07:51 AM
  7. [SOLVED] find the lowest value in a row and add a number to it
    By Kim in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-28-2005, 01:05 PM

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