VBA seems to implicitly convert the data type at runtime.
Consider the following code, which also works.
Sub test2() Dim str As String, arr() As String, num As String Dim i As Integer str = "12 9 30" num = 12 '\\ Note the way number is being passed. arr() = Split(str, " ") For i = LBound(arr) To UBound(arr) If arr(i) = num Then MsgBox (arr(i) & " is equal to " & num) End If Next i End Sub
And then below where the arithmetic operation forces it to be numeric at runtime.
Sub test3() Dim str As String, arr() As String, num As String Dim i As Integer str = "12 9 30" num = 12 arr() = Split(str, " ") For i = LBound(arr) To UBound(arr) If (arr(i) - num) > 0 Then MsgBox (arr(i) & " is greater than " & num) End If Next i End Sub
I know that he will not fully answer your question, but can explain why he gives the correct result. It is advisable to convert to the correct data type, rather than relying on the default values, i.e.
If CInt(arr(i)) > num Then
source share