2012-05-18 2 views
0

각 행을 반복해야하는 Excel 시트가 있으며 특정 조건을 충족하면 데이터가 여러 가지 다른 배열 중 하나에 배치됩니다. 행의 수가 동적입니다. 배열의 길이를 선언 할 때 몇 가지 문제가 발생했습니다. 간단히 행을 반복하고 원하는 조건을 확인하고 조건 A, 조건 B 또는 조건 C에 맞는 행 수를 계속 유지하면서 배열을 redim하는 데 사용할 수 있지만 더 쉬운 방법이 있습니까?조건에 따라 배열에 Excel 행의 내용을 배치하는 방법

감사합니다.

답변

0

"배열의 길이를 선언 할 때의 문제"에 관해서는 알려주지 않습니다. 내 생각 엔 당신이 ReDim으로 변경할 수없는 첫 번째 차원 인 행을 범위에서로드하는 것입니다.

필자는 추측에 따라 다음 두 가지 접근 방식을 제안합니다. 이러한 접근법이 도움이되지 않는다면 완전한 설명으로 되돌아 오십시오.

접근 한 배열 한

로드 전체 범위는 다음의 유형을 기록하는 제 2 어레이를 사용한다.

Dim AllTypes() As Variant 
Dim RowCrnt As Long 
Dim RowType() As Long 

AllTypes = EntireRange.Value 
Redim RowType(LBound(AllTypes,1) TO UBound(AllTypes,1)) 

For RowCrnt = LBound(AllTypes,1) TO UBound(AllTypes,1) 
    ' Classify Row 
    RowType(RowCrnt) = X 
Next 

접근법 2

가변 배열은 당신이 후에 무엇을 더 할 수 있습니다.

는 I는 다음과 같이하는 시트 1을 설정 :

enter image description here

I는 먼저 각 행 적절한 배열의 위치로 분류하는 아래 매크로 달렸다. 그런 다음 각 배열을 즉치 창에 출력하여

a b c d e f g h i j 
b c d e f g h i j k 
c d e f g h i j k l 
a b c d e f g h i j 
b c d e f g h i j k 
c d e f g h i j k l 
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
a 1 b 2 c 3 
b 2 c 3 d 4 
c 3 d 4 e 5 
a 1 b 2 c 3 
b 2 c 3 d 4 
c 3 d 4 e 5 

Sub Test3() 

    Dim ColCrnt As Long 
    Dim InxTypeACrnt As Long 
    Dim InxTypeACrntMax As Long 
    Dim InxTypeBCrnt As Long 
    Dim InxTypeBCrntMax As Long 
    Dim InxTypeCCrnt As Long 
    Dim InxTypeCCrntMax As Long 
    Dim RowCrnt As Long 
    Dim RowLast As Long 
    Dim TypeA() As Variant 
    Dim TypeB() As Variant 
    Dim TypeC() As Variant 

    ReDim TypeA(1 To 2)  ' Change 2 to something sensible 
    ReDim TypeB(1 To 2) 
    ReDim TypeC(1 To 2) 
    InxTypeACrntMax = 0 
    InxTypeBCrntMax = 0 
    InxTypeCCrntMax = 0 

    With Worksheets("Sheet1") 

    RowLast = .Cells(Rows.Count, "A").End(xlUp).Row 

    ' Load each row to the appropriate array 
    For RowCrnt = 1 To RowLast 
     If IsNumeric(.Cells(RowCrnt, "A").Value) Then 
     ' Type B. Five numbers 
     InxTypeBCrntMax = InxTypeBCrntMax + 1 
     If InxTypeBCrntMax > UBound(TypeB) Then 
      ' Array B full. Resize 
      ReDim Preserve TypeB(1 To UBound(TypeB) + 2) 
     End If 
     TypeB(InxTypeBCrntMax) = _ 
         .Range(.Cells(RowCrnt, 1), .Cells(RowCrnt, 5)).Value 
     ElseIf IsNumeric(.Cells(RowCrnt, "B").Value) Then 
     ' Type C. Six values, mixed alpha and numeric 
     InxTypeCCrntMax = InxTypeCCrntMax + 1 
     If InxTypeCCrntMax > UBound(TypeC) Then 
      ' Array C full. Resize 
      ReDim Preserve TypeC(1 To UBound(TypeC) + 2) 
     End If 
     TypeC(InxTypeCCrntMax) = _ 
         .Range(.Cells(RowCrnt, 1), .Cells(RowCrnt, 6)).Value 
     Else 
     ' Type A. Ten strings 
     InxTypeACrntMax = InxTypeACrntMax + 1 
     If InxTypeACrntMax > UBound(TypeA) Then 
      ' Array A full. Resize 
      ReDim Preserve TypeA(1 To UBound(TypeA) + 2) 
     End If 
     TypeA(InxTypeACrntMax) = _ 
         .Range(.Cells(RowCrnt, 1), .Cells(RowCrnt, 10)).Value 
     End If 
    Next 

    End With 

    ' Display contents of each array 

    For InxTypeACrnt = 1 To InxTypeACrntMax 
    For ColCrnt = 1 To 10 
     ' Each element of array TypeA is now a 2D array of size (1 To 1, 1 To 10) 
     ' Note how I access the cells of the inner array 
     Debug.Print TypeA(InxTypeACrnt)(1, ColCrnt) & " "; 
    Next 
    Debug.Print 
    Next 

    For InxTypeBCrnt = 1 To InxTypeBCrntMax 
    For ColCrnt = 1 To 5 
     Debug.Print TypeB(InxTypeBCrnt)(1, ColCrnt) & " "; 
    Next 
    Debug.Print 
    Next 

    For InxTypeCCrnt = 1 To InxTypeCCrntMax 
    For ColCrnt = 1 To 6 
     Debug.Print TypeC(InxTypeCCrnt)(1, ColCrnt) & " "; 
    Next 
    Debug.Print 
    Next 

End Sub 
관련 문제