Awuu
06-05-2017, 01:35 PM
Hello,
I'm new in VBA and I'm looking for help with my problem. I have numbers in A1 to A20 in order from 1 to 20. For number 4.5 I want to get 2 outputs:
-first greater number then 4.5 =>5
-first lower number then 4.5 = >4.
For greater number I found solution but for lower number I don't know to how start looking for first number which is lower then 4.5. In my case is output for lower number 1.
I will be grateful for any help. :)
Sub pr()
a = 4.5
For Each b In Range("A1:A20").Cells
If b.Value > a Then
GreNum = b.Value
Exit For
End If
Next
MsgBox "Greater number is " & GreNum
For Each c In Range("A1:A20").Cells
If c.Value < a Then
LowNum = c.Value
Exit For
End If
Next
MsgBox "Lower number is " & LowNum
End Sub
I'm new in VBA and I'm looking for help with my problem. I have numbers in A1 to A20 in order from 1 to 20. For number 4.5 I want to get 2 outputs:
-first greater number then 4.5 =>5
-first lower number then 4.5 = >4.
For greater number I found solution but for lower number I don't know to how start looking for first number which is lower then 4.5. In my case is output for lower number 1.
I will be grateful for any help. :)
Sub pr()
a = 4.5
For Each b In Range("A1:A20").Cells
If b.Value > a Then
GreNum = b.Value
Exit For
End If
Next
MsgBox "Greater number is " & GreNum
For Each c In Range("A1:A20").Cells
If c.Value < a Then
LowNum = c.Value
Exit For
End If
Next
MsgBox "Lower number is " & LowNum
End Sub