Searching for nearest number

masarup11

New Member
Joined
Oct 2, 2017
Messages
9
Good day, i need help with this one.. i need a loop that will use the lower number inside an array if the

Code:
(searchNum - result) < 0 or negative

for example searchNumber = 9300 / result = 10000 but since it will return as negative result will use 7083

Code:
Dim numbers(), arrItem, maxItem, result, searchNum

        'search number
        searchNum = Cells(x, 3) - Cells(x, 4)
        numbers = Array(1, 4167, 4583, 5417, 7083, 10000, 14583, 25000)
        
        'get max of array item
        maxItem = Application.Max(numbers)


    
        'loop through each array item
        For Each arrItem In numbers


            If Abs(searchNum - arrItem) < maxItem Then
                maxItem = Abs(searchNum - arrItem)
                result = arrItem


                If (searchNum - result) < 0 Then
                    'use lower number in array
                End If


            End If       


        Next arrItem
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You can get your max number more directly this way. First, change the numbers variable from a dynamic array to a normal String variable or, given the way you declare your variables, a Variant, then use this code (here I hard coded the 9300, but you can use whatever method you want to set the searchNum variable's value...
Code:
[table="width: 500"]
[tr]
	[td]searchNum = 9300
numbers = "1,4167,4583,5417,7083,10000,14583,25000"
maxItem = Evaluate("MAX(IF(" & searchNum & ">{" & numbers & "},{" & numbers & "}))")[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Try this:

Code:
Dim numbers(), arrItem, maxItem, result, searchNum
    
'search number
searchNum = 9300
numbers = Array(1, 4167, 4583, 5417, 7083, 10000, 14583, 25000)
'get max of array item
maxItem = Application.Max(numbers)

For i = 0 To UBound(numbers)
    If Abs(searchNum - numbers(i)) < maxItem Then
        maxItem = Abs(searchNum - numbers(i))
        result = numbers(i)
        If (searchNum - result) < 0 Then
        'use lower number in array
            a = numbers(i - 1)
        End If
    
    End If
Next i
 
Upvote 0
It's not totally clear what results you want to return.

But if your numbers are always sorted in ascending order, perhaps a simple:

result = Application.HLookup(searchnum, numbers, True)

also checking that searchnum is not less than the first value in the numbers array.
 
Upvote 0
It's not totally clear what results you want to return.

But if your numbers are always sorted in ascending order, perhaps a simple:

result = Application.HLookup(searchnum, numbers, True)

also checking that searchnum is not less than the first value in the numbers array.


that was my mistake sorry.. i was planning on returning the value of
Code:
result
Cells(x,7) = result 'for example
 
Upvote 0
It's not totally clear what results you want to return.

Thanks, but the question was more about how you wanted to calculate results, rather than where you wanted to put them.

It's clear, e.g. from Rick's and my approaches, that you don't need looping code to get a result.

But do you need VBA code at all? Rather than hardcoding numbers in a VBA sub, why not use an Excel table, allowing quick, dynamic update, and use a simple Excel formula like VLOOKUP to return the desired results to cell(s) of your choice?
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top