使用For循环在VBA中创建新阵列

时间:2019-07-12 01:27:58

标签: arrays excel vba

长时间收听者,首次呼叫者
我需要创建一个数组,其中每个数组的尺寸和数组的数量由用户输入确定。这意味着每次执行代码时,都会创建数量不同的数组,并且每个数组的尺寸可能会有所不同。有没有一种创建for循环的方法,该循环将在VBA中为我创建这些新数组?

例如,假设用户指示需要创建3个数组。然后,它们指示数组1应该为5 x 6,数组2应该为6 x 6,数组3应该为6 x3。我试图找到一种创建for循环的方法,该方法无需我实际就可以执行以下操作创建每个数组:

cell.metadata.hide_input = ! cell.metadata.hide_input;

我想到的是下面的代码。

Dim W1() As Double 
    ReDim W1(5,6)
Dim W2() As Double 
    ReDim W1(6,6)
Dim W3() As Double 
    ReDim W1(6,3)

上面的代码当然会导致错误

  

“编译错误:预期:语句结束”

在VBA中是否可以解决我的问题?还是我没有想到其他解决方法?

任何帮助将不胜感激!这是我的第一个问题,所以请告诉我是否需要分享其他信息。

2 个答案:

答案 0 :(得分:0)

一种实现此目的的方法是使用3维数组。使用此方法,无论所需的数组数量如何,totalArray Dim(加上ReDim)语句仅发生一次。第一个数组维表示数组#。第二维和第三维分别表示每个数组中的x和y维。为了访问数组值,您将需要使用三个嵌套的for循环,totalArray中的每个维度一个。以下是填充每个数组中每个项目并进行调试。打印的子项目。希望这会有所帮助。

Public Sub makeManyArrays()
Dim numOfArrays As Integer
    numOfArrays = 3

Dim xDim() As Double
    ReDim xDim(1 To numOfArrays)
    xDim(1) = 5
    xDim(2) = 6
    xDim(3) = 6
Dim yDim() As Double
    ReDim yDim(1 To numOfArrays)
    yDim(1) = 6
    yDim(2) = 6
    yDim(3) = 3

Dim totalArray() As Double      'only 1 dim statement needed
    ReDim totalArray(1 To numOfArrays, 1 To WorksheetFunction.Max(xDim), 1 To WorksheetFunction.Max(yDim))  'There will be empty values in your array

Dim arrayNum As Integer 'Loop variable
Dim x As Integer                    'Loop variable
Dim y As Integer                    'Loop variable

For arrayNum = 1 To numOfArrays     'Loop through each array
    'You didn't specify what you wanted to do, but I put the x*y value in each cell of each array just for fun.
    For x = 1 To xDim(arrayNum)
        For y = 1 To yDim(arrayNum)
            totalArray(arrayNum, x, y) = x * y
            Debug.Print "Array " & arrayNum & " value at " & x & ", " & y & " is: " & totalArray(arrayNum, x, y)
        Next y
    Next x
Next arrayNum End Sub

答案 1 :(得分:0)

您还可以在数组中构建一个数组。我不确定您可用的变量到底是什么,但这显示了这样一个过程如何工作。 UDF简化了过程。

Sub clickAcceptAndYouWillHaveGoodLuck()
    ReDim bigArray(1 To 3) As Variant


    bigArray(1) = eachARR(0, 4)
    bigArray(2) = eachARR(5, 99)
    bigArray(3) = eachARR(20, 88)


    Debug.Print UBound(bigArray) 'returns 3
    Debug.Print UBound(bigArray(1), 1) 'returns 0
    Debug.Print UBound(bigArray(2), 1) 'returns 5
    Debug.Print UBound(bigArray(2), 2) 'returns 99
    Debug.Print UBound(bigArray(3), 2) 'returns 88

End Sub


Private Function eachARR(xInput As Long, yInput As Long) As Variant
     ReDim bRay(0 To xInput, 0 To yInput) As Variant

     eachARR = bRay


End Function

已更新。我认为以下代码是您尝试执行的更具动态性的示例。我假设ydimxdim是函数?

Sub BetterExample()
    ReDim bigArray(1 To UserINput) As Variant

    Dim r As Long
    For r = 1 To UserINput
        bigArray(r) = eachARR(xdim(r), ydim(r))
    Next r


End Sub


Private Function eachARR(xInput As Long, yInput As Long) As Variant
    ReDim bRay(0 To xInput, 0 To yInput) As Variant

    eachARR = bRay

End Function


Private Function xdim(x As Long) As Long
    'not sure what this is so I just made it itself
    xdim = x + 1

End Function

Private Function ydim(y As Long) As Long
    ydim = y + 2

End Function