VBA highlight closest values in a range

newvbaer

New Member
Joined
Mar 1, 2019
Messages
1
Hi everyone,

I have been trying to figure out a code that would highlight the two closest values of a range selection. What I mean by this is the two closest values (.9999 and .9998 for example) anywhere in a given range and be the two numbers must be adjacent in the range.

I am very new to VBA and I am having a hard time. I was wondering if anyone knew how to do this?

Thank you in advance for your help.
The idea is to find the 2 closest values in the whole table and highlight them. They can be far appart.


 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
A possible solution:
Code:
Sub Closestsss()
Dim DArr(), myRan As String, MaxClos As Long
'
myRan = "K2:K20"    '<<< Value range
MaxClos = 2         '<<< How many highligth
'
ReDim DArr(1 To Range(myRan).Rows.Count)
For i = 1 To UBound(DArr)
    If Range(myRan).Cells(i + 1, 1) <> "" Then DArr(i) = Abs(Range(myRan).Cells(i, 1) - Range(myRan).Cells(i + 1, 1))
Next i
Range(myRan).Interior.ColorIndex = xlNone
For i = 1 To MaxClos
    hpos = Application.Match(Application.WorksheetFunction.Small(DArr, i), DArr, False)
    Range(myRan).Cells(hpos, 1).Interior.ColorIndex = 2 + i
Next i
End Sub
Lines marked <<< need to be adapted to your situation

Bye
 
Upvote 0
Cross posted on multiple sites.

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Please supply the links.
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

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