Hello!
I've written a code that finds the cell closest to 0 in a range, marks it and then uses it in calculations. However, my code works only if the cell closest to 0 is not the first cell. When the closest value is in the first cell, it doesn't mark the cell and the results of the calculations are wrong.
The relevant part of my code
Hopefully someone will spot the mistake and let me know!
I've written a code that finds the cell closest to 0 in a range, marks it and then uses it in calculations. However, my code works only if the cell closest to 0 is not the first cell. When the closest value is in the first cell, it doesn't mark the cell and the results of the calculations are wrong.
The relevant part of my code
Code:
Dim mx As Single 'maximum value in the range [I]rng[/I]
Dim cell As Range 'each cell in the range [I]rng[/I]
Dim phase As String 'cell with the maximum value in the range [I]rng[/I]
Const zero As Integer = 0 'the value I want the cell to be the closest to
Set rng = Range(Cells(row, i), Cells(j, i))
mx = Application.Max(rng)
For Each cell In rng
If Abs(zero - cell) < mx Then
mx = Abs(zero - cell)
phase = cell.Address
End If
Next cell
Range(phase).Offset(, 1) = "x" 'to mark the cell
Cells(22, i + 1) = (Range(phase).Offset(0, -3).Value) - ((((Range(phase).Value) - zero) / ((Range(phase).Value) - (Range(phase).Offset(1, 0).Value))) * ((Range(phase).Offset(0, -3).Value) - (Range(phase).Offset(1, -3).Value))) 'the calculation
Next i
Hopefully someone will spot the mistake and let me know!