I'm working on a VBA problem to find the maximum and minimum values in a sequence while skipping invalid inputs.
The issue I'm facing is that when the first value in the sequence is invalid, the variable minnum automatically gets assigned a value of 0 (for example: entering 3, a, 1, 2 gives min = 0, max = 2).
However, this error doesn't occur if the first input is valid (for example: entering 3, 1, a, 2 gives min = 1, max = 2).
Sub minmaxnum()
Dim maxnum As Long
Dim minnum As Double
Dim n As Integer
Dim num As Double
Dim i As Integer
n = InputBox("How many numbers do you want to enter?")
maxnum = 0
minnum = 1E+39
On Error Resume Next
For i = 1 To n
num = InputBox("Enter those numbers: ")
If num > maxnum Then
maxnum = num
End If
If num < minnum Then
minnum = num
End If
Next i
MsgBox "Maxnum is: " & maxnum & vbCrLf & "Minnum is: " & minnum
End Sub