Capt_Antihero
New Member
- Joined
- Jun 16, 2014
- Messages
- 14
Hello,
I have modified some code I found in these forums that once I enter a value in a cell I can have it find the closest match to that value and scroll down the list and paste that value as an offset. The code works perfectly, but my data will often contain the same values repeated and these aren't duplicates and I need to know how to loop the code to ensure it doesn't just find the first value but also the second (if the first is already populated) Code is:
Sub findclose()
Dim rng As Range, Dn As Range, Mx As Single, oAd As String
Dim num As Range
Set num = ActiveSheet.Range("B1")
Set rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
Mx = Application.Max(rng)
For Each Dn In rng
If Abs(num - Dn) < Mx Then
Mx = Abs(num - Dn)
oAd = Dn.Address
End If
Next Dn
Range(oAd).Offset(, 2) = ActiveSheet.Range("B1").Value
---- What I need to do is insert a loop of some sort that basically says IF my offset cell has a value then to find the next closest amount and continue the macro.
So if my list was:
700
50
500
600
500
And I wanted to find 499 then my list would look like:
700
50
500 499
600
500
BUT if I wanted to match off another amount of 501 it would keep the 499 it found and then find the 501 making the completed list look like:
700
50
500 499
600
500 501
Right now the amounts are being overidden as I don't know how to loop it... Please help if you can...?
James.
I have modified some code I found in these forums that once I enter a value in a cell I can have it find the closest match to that value and scroll down the list and paste that value as an offset. The code works perfectly, but my data will often contain the same values repeated and these aren't duplicates and I need to know how to loop the code to ensure it doesn't just find the first value but also the second (if the first is already populated) Code is:
Sub findclose()
Dim rng As Range, Dn As Range, Mx As Single, oAd As String
Dim num As Range
Set num = ActiveSheet.Range("B1")
Set rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
Mx = Application.Max(rng)
For Each Dn In rng
If Abs(num - Dn) < Mx Then
Mx = Abs(num - Dn)
oAd = Dn.Address
End If
Next Dn
Range(oAd).Offset(, 2) = ActiveSheet.Range("B1").Value
---- What I need to do is insert a loop of some sort that basically says IF my offset cell has a value then to find the next closest amount and continue the macro.
So if my list was:
700
50
500
600
500
And I wanted to find 499 then my list would look like:
700
50
500 499
600
500
BUT if I wanted to match off another amount of 501 it would keep the 499 it found and then find the 501 making the completed list look like:
700
50
500 499
600
500 501
Right now the amounts are being overidden as I don't know how to loop it... Please help if you can...?
James.