你如何在 VBA 中重新定义一个数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/890709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:47:18  来源:igfitidea点击:

How do you redimension an array in VBA?

ms-accessvba

提问by Tony Toews

I'm attempting to rediminsion an array in MS Access VBA. What is the most efficient way to do this?

我正在尝试在 MS Access VBA 中重新定义一个数组。执行此操作的最有效方法是什么?

回答by Curtis Inderwiesche

How about...

怎么样...

This will preserve data already in MyArray

这将保留 MyArray 中已有的数据

 Redim Preserve MyArray(15)

This will erase any previous data existing in MyArray

这将删除 MyArray 中存在的任何先前数据

 Redim MyArray(15)

回答by Mark Nold

The most efficientway to redimension an array is to limit the number of times you resize that array. Every time you resize the array VB will take the whole array and copy it, wasting time and memory.

重新调整数组大小的最有效方法是限制调整数组大小的次数。每次调整数组大小时,VB 都会获取整个数组并复制它,浪费时间和内存。

If you don't know the size of your array at development time you should make the best guess to the maximum size of the array, then fill the array. Once finished filling the array you can resize it down to the correct size.

如果您在开发时不知道数组的大小,您应该对数组的最大大小做出最佳猜测,然后填充数组。完成填充数组后,您可以将其调整为正确的大小。

In loops it's often best to make this guess by doubling the size of the current array once you run out of space. You can see this in action below with RedimTestA()resizing the array each iteration (1,000,000 times) and RedimTestB()only resizing it occasionally (22 times).

在循环中,一旦空间不足,通常最好通过将当前数组的大小加倍来进行这种猜测。您可以在下面通过RedimTestA()每次迭代调整数组大小(1,000,000 次)和RedimTestB()偶尔调整数组大小(22 次)来看到这一点。

On my laptop RedimTestA()takes 3.93 seconds and RedimTestB()takes 0.41 seconds.

在我的笔记本电脑上RedimTestA()需要 3.93 秒和RedimTestB()0.41 秒。

Option Explicit

Sub RedimTest()
  Dim tA, tB As Single
  tA = RedimTestA(1000000)
  tB = RedimTestB(1000000)

  MsgBox "Test A takes : " & tA & ", and Test B takes : " & tB

End Sub


Function RedimTestA(iterations As Long) As Single
  Dim t As Single
  Dim i As Long
  Dim aryString() As String
  Dim myString As String

  t = Timer
  Do While i <= iterations
    ReDim Preserve aryString(i) As String
    aryString(i) = "ABCEFG123"
    i = i + 1
  Loop
  RedimTestA = Timer - t

End Function


Function RedimTestB(iterations As Long) As Single
  Dim t As Single
  Dim i As Long
  Dim aryString() As String
  Dim myString As String

  t = Timer

  ReDim aryString(0) As String
  Do While i <= iterations
    If i >= UBound(aryString) Then
      ReDim Preserve aryString(i * 2) As String
    End If

    aryString(i) = "ABCEFG123"
    i = i + 1
  Loop

  ReDim Preserve aryString(i - 1) As String ' i - 1 becuase of the final i = i + 1
  RedimTestB = Timer - t

End Function

回答by Tony Toews

Also note that you can only redim the right most dimension of a multi dimensional array.

另请注意,您只能重新调整多维数组最右边的维度。