# Thread: How to find the closest value from textboxes in array

1. ## How to find the closest value from textboxes in array

I've been trying to find the proper coding for this game project but not luck at all. I'm still hoping to find someone to help me achieve this project.

I have 5 textboxes, each will have a numeric value from 1 to 50 randomly.
My point is I have the to find the closest value to my 6th textbox which will hold another numeric value from 1 to 50. The closest value entered in the 5 textboxes to the textbox6 should be copied in another textbox7.
I would really appreciate your help thanks.

2. ## Re: How to find the closest value from textboxes in array

Code:
```    Dim iSmallestDiff As Single
Dim iClosestValue As Single
Dim c As Long
Dim txt As TextBox
Dim iDiff As Single
Dim iSet As Boolean

For c = 1 To 5 ' it will scan the 5 textboxes, from Text1 to Text5
Set txt = Me.Controls("Text" & CStr(c)) ' sets the textbox controls Text1 to text5 to a variable, one at a time
iDiff = Abs(Val(txt.Text) - Val(Text6.Text)) ' in the iDiff variable, it puts the difference that the value of the current textbox has with the value of Text6
If (iDiff < iSmallestDiff) Or (iSmallestDiff = 0) And Not iSet Then ' if the difference is less than the previous, or if it is zero because it is the first (it is Text1), the variable iSet will be False and it will enter too
iSmallestDiff = iDiff ' remember this difference that is now the smallest, for the next time
iClosestValue = Val(txt.Text) ' the current value that is closest to the value of Text6 is this, for now
iSet = True ' it means that the variable iSmallestDiff has a meaningful value (If iSet is True, it means that if iSmallestDiff has zero it is not because it is just starting)
End If
Next c
Text7.Text = iClosestValue ' write the closest value into Text7```

3. ## Re: How to find the closest value from textboxes in array

Originally Posted by Eduardo-
Code:
```    Dim iSmallestDiff As Single
Dim iClosestValue As Single
Dim c As Long
Dim txt As TextBox
Dim iDiff As Single
Dim iSet As Boolean

For c = 1 To 5 ' it will scan the 5 textboxes, from Text1 to Text5
Set txt = Me.Controls("Text" & CStr(c)) ' sets the textbox controls Text1 to text5 to a variable, one at a time
iDiff = Abs(Val(txt.Text) - Val(Text6.Text)) ' in the iDiff variable, it puts the difference that the value of the current textbox has with the value of Text6
If (iDiff < iSmallestDiff) Or (iSmallestDiff = 0) And Not iSet Then ' if the difference is less than the previous, or if it is zero because it is the first (it is Text1), the variable iSet will be False and it will enter too
iSmallestDiff = iDiff ' remember this difference that is now the smallest, for the next time
iClosestValue = Val(txt.Text) ' the current value that is closest to the value of Text6 is this, for now
iSet = True ' it means that the variable iSmallestDiff has a meaningful value (If iSet is True, it means that if iSmallestDiff has zero it is not because it is just starting)
End If
Next c
Text7.Text = iClosestValue ' write the closest value into Text7```

Thanks man you are amazing everything is almost done with my project. This code is amazing! Is just that now I just need to modify it to count up from 0 to 9 in loop, and find the closest number disregarding an equal number only the ascending closest number. Like if I enter any value from text1 to text5.
Example:
Text1 = 4
Text2 = 0
Text3 = 1
Text4 = 9
Text5 = 2

Text6 = 9

Text7 = 0
It must search in ascending order from 0 to 9 loop disregarding or no counting the same number like 9 but it went to 0.
The looping count should always be
0 1 2 3 4 5 6 7 8 9. Thanks once again dude.

4. ## Re: How to find the closest value from textboxes in array

Try to do it yourself, how would you modify the code for doing that? (please post here what you would do)

5. ## Re: How to find the closest value from textboxes in array

Originally Posted by Eduardo-
Try to do it yourself, how would you modify the code for doing that? (please post here what you would do)
I already did modified by changing greater than symbols but I can't figure out the code for not counting the same number and looping 0 to 9, thanks anyway.

6. ## Re: How to find the closest value from textboxes in array

If by "an equal number" you mean to disregard equal number to Text6:

Code:
```    Dim iSmallestDiff As Single
Dim iClosestValue As Single
Dim c As Long
Dim txt As TextBox
Dim iDiff As Single
Dim iSet As Boolean

For c = 1 To 9
Set txt = Me.Controls("Text" & CStr(c))
iDiff = Abs(Val(txt.Text) - Val(Text6.Text))
If iDiff <> 0 Then
If (iDiff < iSmallestDiff) Or (iSmallestDiff = 0) And Not iSet Then
iSmallestDiff = iDiff
iClosestValue = Val(txt.Text)
iSet = True
End If
End If
Next c
Text7.Text = iClosestValue```
If by "an equal number" you mean when there are more than one closest to take the last one:

Code:
```    Dim iSmallestDiff As Single
Dim iClosestValue As Single
Dim c As Long
Dim txt As TextBox
Dim iDiff As Single
Dim iSet As Boolean

For c = 1 To 9
Set txt = Me.Controls("Text" & CStr(c))
iDiff = Abs(Val(txt.Text) - Val(Text6.Text))
If (iDiff <= iSmallestDiff) Or (iSmallestDiff = 0) And Not iSet Then
iSmallestDiff = iDiff
iClosestValue = Val(txt.Text)
iSet = True
End If
Next c
Text7.Text = iClosestValue```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured