동적 배열을 사용할 때 vba의 각 루프가 올바른 수의 요소를 반환하지 않는 이유를 알지 못합니다.각 루프에 대한 동적 배열 요소의 번호가 잘못되었습니다.
exemple를 들어, 내 배열의 크기는 4이며, 실제로 그것의 더 나은 명시 적으로 t_direction
0 To 4
로 선언
Public Sub test()
Dim t_direction() As String
Dim t_nextDirection() As String
Dim myDirection As Variant
Dim test As Integer
Var = 0
ReDim t_direction(4)
t_direction(0) = "N"
t_direction(1) = "S"
t_direction(2) = "E"
t_direction(3) = "W"
t_nextDirection = randomizeArray(t_direction)
For Each myDirection In t_nextDirection
Var = Var + 1
Next myDirection
MsgBox (UBound(t_nextDirection))
MsgBox (Var)
End Sub
Public Function randomizeArray(ByVal t_array As Variant) As String()
Dim i As Integer
Dim j As Integer
Dim tmp As String
Dim numItems As Integer
numItems = UBound(t_array) - 1
' Randomize the array.
For i = 0 To numItems
' Pick a random entry.
j = Rand(0, numItems)
' Swap the numbers.
tmp = t_array(i)
t_array(i) = t_array(j)
t_array(j) = tmp
Next i
'MsgBox (UBound(t_array))
randomizeArray = t_array
End Function
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Integer
Rand = Int((High - Low + 1) * Rnd) + Low
End Function
멋진 샷, 언제나처럼 :) 그리고 5000+에 대한 축하, 당신은 매우 빨리 거기에있어. – JMax
@Jmax를 축하해 주셔서 대단히 감사합니다. – brettdj