0

This statement answer No,Can I answer Yes? Is Arr(0) return 2 But 2 not equal 2.I think so A_arr as string how to replace it to Integer in array?

Private Sub aaa()
    Dim aaa As String
    A = "2,3,4"
    A_arr = Split(A, ",")
    MsgBox A_arr(0)

    Dim B_arr As Variant: B_arr = Array(2, 3, 4)
    MsgBox B_arr(0)

    If A_arr(0) = B_arr(0) Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • `If Int(A_arr(0)) = B_arr(0) Then` But that would just return it as an integer for that check, not actually turn it into an integer in the array – Christofer Weber Aug 28 '21 at 19:22
  • This has nothing to do with arrays and everything to do with Variants. `Dim a, b: a = 2: b = "2": Msgbox a = b` => False. `Dim a as long, b as string: a = 2: b = "2": Msgbox a = b` => True. – GSerg Aug 28 '21 at 19:23
  • Technically a duplicate of https://stackoverflow.com/q/18678481/11683, but it's [not very satisfactory](https://stackoverflow.com/questions/18678481/why-in-excel-comparing-1-or-comparing-any-number-to-any-text-with-results-in#comment27518694_18681599). – GSerg Aug 28 '21 at 19:30
  • @AtipongChamnanjan https://stackoverflow.com/q/44620554/11683 – GSerg Aug 28 '21 at 19:33
  • 1
    This is actually the first time I see an important difference in how the *same* code works in VBA and VBScript. `"2" = 2` is True in both VBA and VBScript, but `CStr(2) = CLng(2)` is True in VBA and False in VBScript (for the same Variant comparison reasons). – GSerg Aug 28 '21 at 19:58

1 Answers1

-1

To replace values in A_arr with their integer equivalents:

Option Explicit
Private Sub aaa()
Dim A As String
Dim A_arr, B_arr, C_arr
Dim I As Long

A = "2,3,4"
A_arr = Split(A, ",")
MsgBox A_arr(0)
 
B_arr = Array(2, 3, 4)
MsgBox B_arr(0)

If A_arr(0) = B_arr(0) Then
    MsgBox "YES"
Else
    MsgBox "NO"
End If

'====================================
'change A_arr from string to numbers
ReDim C_arr(LBound(A_arr) To UBound(A_arr))
For I = LBound(A_arr) To UBound(A_arr)
    C_arr(I) = CLng(A_arr(I))
Next I
A_arr = C_arr

If A_arr(0) = B_arr(0) Then
    MsgBox "YES"
Else
    MsgBox "NO"
End If

End Sub
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60