같은 모듈의 다른 하위에 배열을 전달해야하는 워크 시트에 모듈이 있습니다. 지금까지, 각 개별 배열 요소를 가져 오는 데 사용되는 변수 N이 항상 조사 식 창에서 0으로 표시된다는 것을 알았습니다. 배열에서 요소를 가져 오는 방법은 무엇입니까? 다음은 코드입니다.ByRef를 하위로 전달한 후 배열에 값이 없습니다.
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
고마워요!
'If size> 7 Then size = 0'의 목적은 무엇입니까? if 테스트가 전달되는 열의 경우 배열 크기 (0 ~ 0) (즉 1 요소)가됩니다. –
@Cor_Blimey 내일까지 끝내야하기 때문에 주변에서 어색한 일이 일어납니다. 기본적으로 행 중 일부는 너비가 1 열이고 'Range (ActiveCell.Offset (0, 1), ActiveCell.Offset (0, 1) .End (xlToRight)). Columns.Count - 1'을 그들 크기는 16k에서 올라간다. 어떤 잘못입니다. –
다음 sub로 전달하기 전에 array departmentNums에 debug.print를 수행하십시오. 전달하려는 값이 들어 있는지 확인하십시오. – Alex