2012-01-02 1 views
1

동적 배열을 사용할 때 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 

답변

4


ReDim t_direction(4)
를 사용하여 5 개 요소 배열을 만드는 중입니다. 15,둘 중

  • 가 4 소자 어레이를 ReDim t_direction(3) (즉, 0-3)를 생성하고 그와 일치 numItems 사용하거나
  • 가 4 소자 어레이를 생성한다

    t_direction(0) 같이, 제 1 요소가 발생할 때 ReDim t_direction을 1의 밑수 (즉, 1 ~ 4)로 사용하고 numItems (즉 numItems = UBound(t_array))과 일치시킵니다. Option Base 1 아래로 첫 번째 요소를 강제로 다음 ReDim t_direction(1 To 4)

아래 코드는 나중에 접근 방식을 사용을 사용하여 anyow 보장 1 (. 그것은 4 반환하고 4보다는 현재 4, 5

Option Base 1 
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(1 To 4) 

t_direction(1) = "N" 
t_direction(2) = "S" 
t_direction(3) = "E" 
t_direction(4) = "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) 

    ' Randomize the array. 
    For i = 1 To numItems 
     ' Pick a random entry. 
     j = Rand(1, 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 
+0

멋진 샷, 언제나처럼 :) 그리고 5000+에 대한 축하, 당신은 매우 빨리 거기에있어. – JMax

+0

@Jmax를 축하해 주셔서 대단히 감사합니다. – brettdj

2

ReDim t_direction(4) 각 루프에서 5 반복 ...이 :

ReDim t_direction(0 To 3) 

지정된 하한이없는 경우 (To 절 사용), 기본값 lower bound가 사용됩니다.
이 기본값은 모듈 수준에서 Option Base {0|1}을 사용하여 0 또는 1으로 설정할 수 있습니다. 당신이 하한으로 0 또는 1 국한되지 않습니다 VBA에서

, 당신은 당신이 원하는 모든 값을 사용할 수 있습니다 : Option Base가없는
는 기본 기본값은 0

노트입니다.

배열의 항목 수는

numItems = UBound(arr) - LBound(arr) + 1 

당신이 하한이

무엇인지에 대한 어떤 가정을하지 않는이 방법을 사용하여 계산하기

For i = LBound(arr) To UBound(arr) 

을 사용하여 배열을 반복하려면

+0

이 단일 변경을하면 문제가 해결되지 않으며 단순히 문제를 4/5에서 3/4로 이동합니다. – brettdj

+0

@Brettdj 문제의 근본 원인은 'Redim'작업 및 하한 제한에 대한 잘못된 가정입니다. 대답은 단순히 코드를 디버깅하는 것보다 루트 문제를 해결하려고 시도합니다 –

+0

원래 응답은 루트 문제 * 'numItems '바운딩의 불일치를 언급하지 않았기 때문입니다. – brettdj

관련 문제