2012-01-02 55 views
1

我不明白为什么每个vba循环都没有返回当我使用动态数组时元素的好数目。每个循环动态数组中的元素数量不好

对于为例,我的数组大小是4,我在每个循环迭代5 ...

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

目前您正在创建一个5元素阵列
ReDim t_direction(4)
作为发生第一个元素作为t_direction(0)

你应该要么

  • 创建一个4元件阵列ReDim t_direction(3)(即0至3),然后使用numItems符合的是,或
  • 创建一个4元件阵列ReDim t_direction与1(即1到4)的基数,然后使用numItems一致(即numItems = UBound(t_array))。下面Option Base 1迫使第一元件为1(其然后通过使用ReDim t_direction(1 To 4)

下面的代码使用后一种方法确保anyow,它返回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 2012-01-02 07:37:47

+0

@Jmax thx祝贺 - 非常感谢:) – brettdj 2012-01-02 08:59:00

2

ReDim t_direction(4)实际上宣告t_direction0 To 4

其更好地是明确的:

ReDim t_direction(0 To 3) 

如果没有指定的下限(使用To子句),则默认的下限绑定被使用。
通过在模块级别使用Option Base {0|1},可以将此默认设置设置为01
在没有Option Base那么默认默认为0

注:

在VBA中,你不仅限于01作为下界,你可以使用任何你想要的值。

遍历数组使用

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

要计算出数组中元素的个数使用

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

这样,你是不是在下界是什么

做任何假设
+0

做这个单一的修改并不能解决问题,它只是将问题从4/5移到3/4 – brettdj 2012-01-02 00:36:08

+0

@Brettdj问题的根源在于对'Redim'的操作和对下界的低估的错误假设。答案是试图解决根本问题,而不是简单地调试他的代码 – 2012-01-02 00:47:52

+0

你的原始答案只是部分解决了*根问题*因为它没有解决numItems边界的不一致问题。 – brettdj 2012-01-02 00:56:10

相关问题