将ByRef传递给sub后,Array没有值

时间:2014-11-10 22:15:21

标签: arrays vba excel-vba excel

我在工作表中有一个模块,它应该将数组传递给同一模块中的另一个子模块。到目前为止,我注意到用于拉动每个单独数组元素的变量N总是在监视窗口中显示0,如何从阵列中取出我的元素?这是代码:

Option Explicit

Sub CreateReports()

Dim numRows As Integer
Dim numCount As Integer
Dim category As String
Dim size As Integer
Dim sizeCount As Integer

Dim departmentNums() As Integer
With Sheets("GM Alignment")
    numRows = Application.WorksheetFunction.CountA(Range("A2:A1048576"))
    .Range("A2").Select
    Do While numCount < numRows
        category = ActiveCell.Value
        size = Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 1).End(xlToRight)).Columns.Count - 1
        If size > 7 Then
            size = 0
        End If
        ReDim departmentNums(size)
        .Cells(ActiveCell.Row, 1).Select
        For sizeCount = 0 To size
            ActiveCell.Offset(0, 1).Select
            departmentNums(sizeCount) = ActiveCell.Value
        Next sizeCount
        .Cells(ActiveCell.Row, 1).Select
        GenerateReports Arr:=departmentNums, Sheet:=category
        ActiveCell.Offset(1, 0).Select
    numCount = numCount + 1
    Loop
End With

End Sub

Sub GenerateReports(ByRef Arr() As Integer, Sheet As String)

Dim N As Integer

For N = LBound(Arr) To UBound(Arr)

    Dim Lastrow As Long

    With Sheets("DATA")

        If .Range("I:I").Find(N, , xlValues, xlWhole, , , False) Is Nothing Then
            MsgBox "No " + Sheet + " rows found. ", , "No Rows Copied": Exit Sub
        Else

            Application.ScreenUpdating = False

            Lastrow = .Range("K" & Rows.Count).End(xlUp).Row
            .Range("K1:K" & Lastrow).AutoFilter Field:=1, Criteria1:=N
            .Range("K2:K" & Lastrow).SpecialCells(xlCellTypeVisible).EntireRow.Copy
            Sheets(Sheet).Range("A2").PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False
            .AutoFilterMode = False

            'Position on cell A3
            With Application
                .CutCopyMode = False
                .Goto Sheets("DATA").Range("A2")
                .ScreenUpdating = True
            End With

            MsgBox "All matching data has been copied.", , "Copy Complete"

        End If

    End With

Next N

End Sub

谢谢!

1 个答案:

答案 0 :(得分:0)

我的代码并不是真的太过分了,但我注意到你在Arr中没有读过或分配给GenerateReports。我相信你误解了Arrays的行为方式。

Dim index as Integer
For index = LBound(Arr) To UBound(Arr)
    Debug.Print "Index: "; index; " Value: " Arr(index)
Next N

LBound(Arr)Ubound(Arr)返回Arr的最低和最高索引,而不是值。要访问Arr使用Arr(index)

中包含的值

如果您不关心索引,可以使用

   Dim element as Integer
   For each element in Arr
      debug.Print element
   Next n

建议尽可能使用此方法,因为它允许使用其他序列,例如Collection。但是,并不总是可能,例如当您迭代多个序列或序列的一部分时。

这是一个基本的例子:

Sub PrintOneToTen()
   Dim xs(1 to 10) as Integer
   FillArray xs
   Dim x as Integer
   For Each x In xs
       Debug.Print x
   Next x
   ' or just Debug.Print Join(xs, vbNewLine)
End Sub
Sub FillArray(ByRef xs() As Integer)
   Dim i as Integer
   For i = LBound(xs) To Ubound(xs)
       xs(i) = i
   Next i
End Sub

列出了here的VB.NET阵列文档。注意VB.NET不是VBA,但就数组而言,99%的信息在VBA中应该是相同的。我会发布VBA文档,但它深埋在微软的“微软”中。数据库,我不相信他们关心它。