【问题标题】:How to repeatedly define arrays with numbered names in VBA?如何在 VBA 中重复定义带有编号名称的数组?
【发布时间】:2019-11-22 16:53:18
【问题描述】:

我正在尝试编写一个重复定义数组的 for 循环,但每个循环使用不同的数组名称。 例如:

for i = 1 to 10
   dim array"i"(1 to 5) as long
loop

这里,我不知道如何将“i”转换为1,2,3....10,这样就会有array1,array2,array3....array10。关于这如何工作的任何想法?

【问题讨论】:

  • 你不能。你到底想做什么?
  • 数组列表可能是最好的解决方案。
  • 为什么不是二维数组? Dim Arr(1 to 10, 1 to 5) 然后你可以调用它 Arr(2,4) 它返回第二个数组中的第 4 项。
  • 好点@Scott Craner,我会试试的,谢谢!
  • 或者甚至是数组的集合,这取决于你想对数组做什么。

标签: excel vba


【解决方案1】:

数组数组

Vityata 提出的创建数组列表(又名锯齿状数组数组数组)可能接近您的需求并执行 sub 调用以重新调整任何“子”数组的大小。

    For idx = 1 To 10               ' << example loop from idx = 1 to n
        Standardize a(idx), 1, 5    ' << sub call redimension e.g. from 1 to 5
    Next idx

主要程序包括部分数组项的测试显示

注意:分配了一些数字和字母测试值,减少了子数组和循环的数量,并使用常量来引用子数组(例如,通过 a(NUMS)a(ALPHA));单个项目,例如a(ALPHA)(5) 因此将返回一个字符串值“五”(注意特殊语法!)。

Sub ArrayOfArrays()
  ' [0] provide for a variant container array
    Dim a()
  ' [1] fill container with empty "sub" arrays
  '      (note that first sub array remains empty just to allow 1-based enumeration)
    a = Array(Array(), Array(), Array())
  ' [2] assign values to variant "sub" arrays - temporarily zero-based
  '     (note that at this point you are assigning arrays of different lengths)
    Const NUMS& = 1, ALPHA& = 2       ' << change to your needs
    a(NUMS) = Array(1, 2, 3, 4)       ' << test items
    a(ALPHA) = Array("one", "two", "three", "four", "five", "six")

  ' [3] >> standardize all "sub" arrays to the same boundaries 1 To 6 in a loop
  '     (note that you'd change here from 0-based to 1-based "sub" arrays)
    Dim idx&
    For idx = NUMS To ALPHA         ' << example loop from idx = 1 to 2
        Standardize a(idx), 1, 6    ' << sub call redimension from 1 to 6
    Next idx

  ' ================= Some display examples ===============================
  ' [4a] display some test items from NUMS and ALPHA (including empty ones)
  '     (note the differing syntax)
    Debug.Print "* [4a] Display test items:"

    Debug.Print "* NUMS - " & UBound(a(NUMS)) & " items: ", _
                Join(a(NUMS), ",")
    Debug.Print "  e.g. a(NUMS)(1) =" & a(NUMS)(1)   '--> 1
    Debug.Print "  e.g. a(NUMS)(2) =" & a(NUMS)(2)   '--> 2

    Debug.Print "* ALPHA - " & UBound(a(ALPHA)) & " items: ", _
                Join(a(ALPHA), ",")
    Debug.Print "  e.g. a(ALPHA)(5) =" & a(ALPHA)(5) '--> "five"
    Debug.Print "  e.g. a(ALPHA)(6) =" & a(ALPHA)(6) '--> "six"

  ' [4b] display each first and last item of each category
    Debug.Print "* [4b] Display first and last item of each category:"
    Dim i&, first&, last&, categories$
    categories = "Dummy,Nums,Alpha"
    For i = NUMS To ALPHA
        first = LBound(a(i)): last = UBound(a(i))   ' 1 to 6
        Debug.Print "  " & Split(categories, ",")(i) & ": " & vbTab & _
                    "1st item: |" & a(i)(first) & "|," & vbTab & _
                    "last item: |" & a(i)(last) & "|"
    Next i

End Sub

转接电话Standardize

Sub Standardize(arr, lowerBoundary&, upperBoundary&)
' Purpose: change jagged array boundaries by preserving included items
' Note:    argument arr is passed ByRef (by default)
    Dim tmpArr
    tmpArr = arr
    ReDim Preserve tmpArr(lowerBoundary To upperBoundary)
    arr = tmpArr
End Sub

测试显示在 VBEditor 的即时窗口中

* [4a] Display test items:
* NUMS - 6 items:           1,2,3,4,,
  e.g. a(NUMS)(1) =1
  e.g. a(NUMS)(2) =2
* ALPHA - 6 items:          one,two,three,four,five,six
  e.g. a(ALPHA)(5) =five
  e.g. a(ALPHA)(6) =six
* [4b] Display first and last item of each category:
  Nums:     1st item: |1|,  last item: ||
  Alpha:    1st item: |one|,    last item: |six|

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2012-03-29
    • 1970-01-01
    • 2021-06-28
    • 2021-12-23
    • 1970-01-01
    • 2021-02-23
    相关资源
    最近更新 更多