Search code examples
arraysvbacomparisonequals

VBA two identical numbers are not equal


I don't see why the output values are not equal through they seem identical. Probably it is something so obvious that I fail to see it. Parts of the code are not mine and I do not entirely understand it.

Sub main()
Dim elements As Variant, vresult As Variant
Dim i As Long
Dim combolist As Variant
ReDim combolist(0)
stringg = "0aaaaaaa"
ReDim elements(Len(stringg))
For k = 1 To Len(stringg)
elements(k) = k
Next
MsgBox Join(elements, ",")
For i = 1 To UBound(elements)
ReDim vresult(1 To i)
Call Combinations(elements, i, vresult, 1, 1, combolist)
Next i
Call checkk(combolist, stringg, elements)
End Sub

Sub Combinations(elements, p, vresult, startpos, index, combolist)
Dim i As Long


For i = startpos To UBound(elements)
vresult(index) = elements(i)
If index = p Then
    combolist(UBound(combolist)) = Join(vresult, ",")
    ReDim Preserve combolist(UBound(combolist) + 1)
Else
    Call Combinations(elements, p, vresult, i + 1, index + 1, combolist)
End If
Next i

End Sub
Sub checkk(combolist, stringg, elements)
Dim q As Long, r As Long
For k = LBound(combolist) To UBound(combolist)
getpos = Split(combolist(k), ",")
For q = 1 To UBound(elements)
For r = LBound(getpos) To UBound(getpos)
MsgBox elements(q) & " - " & getpos(r)
If elements(q) = getpos(r) Then
MsgBox "found"
End If
Next
Next
Next
End Sub

Solution

  • You should define all of your variables including your arrays. One is probably an integer another a decimal or maybe there is an issue with upper case and lower case. There's really not a lot of information in your code to give much information.

    I would step through your code and set it to stop when you know they should equal each other...

    Here's a tool to try to debug your last routine and you can hopefully trouble shoot it yourself:

    Sub checkk(combolist, stringg, elements)
    Const theNUmberYouKnow as double = 4
    
    Dim q As Long, r As Long
    For k = LBound(combolist) To UBound(combolist)
    getpos = Split(combolist(k), ",")
    For q = 1 To UBound(elements)
    
    
    For r = LBound(getpos) To UBound(getpos)
    MsgBox elements(q) & " - " & getpos(r)
    If elements(q) = getpos(r) OR elements(q) = theNUmberYouKnow Then
    
            msgbox "R = " & getpos(r)
            Stop
    
    
    MsgBox "found"
    End If
    Next
    Next
    Next
    End Sub