VBA两个相同的数不相等
VBA two identical numbers are not equal
我不明白为什么输出值不相等,因为它们看起来相同。可能它是如此明显,以至于我看不到它。
部分代码不是我的,我也不完全理解。
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
您应该定义所有变量,包括您的数组。一个可能是整数,另一个可能是小数,或者大写和小写可能存在问题。您的代码中确实没有太多信息可以提供很多信息。
我会单步执行您的代码并将其设置为在您知道它们应该彼此相等时停止...
这里有一个工具可以用来调试你的最后一个例程,希望你可以自己解决问题:
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
我不明白为什么输出值不相等,因为它们看起来相同。可能它是如此明显,以至于我看不到它。 部分代码不是我的,我也不完全理解。
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
您应该定义所有变量,包括您的数组。一个可能是整数,另一个可能是小数,或者大写和小写可能存在问题。您的代码中确实没有太多信息可以提供很多信息。
我会单步执行您的代码并将其设置为在您知道它们应该彼此相等时停止...
这里有一个工具可以用来调试你的最后一个例程,希望你可以自己解决问题:
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