VBA锯齿状阵列到范围

时间:2016-12-20 19:38:53

标签: vba excel-vba jagged-arrays excel

我的宏遍历一个范围,按列循环,查找数值数据在每列中的开始位置,并将范围存储在锯齿状数组中(代码中的“矩阵”变体)。

之后,我想将整个矩阵返回到另一个工作表中的范围。如果我尝试将“matrix(1)”指定给我想要放置的范围,它可以正常工作,但如果我尝试将整个“矩阵”分配给一个范围,我会得到空白单元格。

如何在不使用循环的情况下将“矩阵”中的所有值一次性返回到某个范围?

这是源数据,代码循环通过它: enter image description here

我想将“矩阵”的所有行都返回为: enter image description here

这是我的代码:

       Sub MyMatrix()

        Dim wb1 As Workbook
        Set wb1 = ActiveWorkbook

        Dim wsNSA As Worksheet
        Set wsNSA = wb1.Worksheets("NSA")

        Dim wsSA As Worksheet
        Set wsSA = wb1.Worksheets("SA")

        Dim col As Range

        Dim matrix() As Variant


        'LR is the Last row and LC is the last column with data
        LR = wsNSA.Cells(1, 1).End(xlDown).Row
        LC = wsNSA.Cells(LR, 1).End(xlToRight).Column

        'Loops through columns and finds the row where numeric data begins
        For Each col In wsNSA.Range(wsNSA.Cells(1, 2), wsNSA.Cells(LR, LC)).Columns
        wsNSA.Activate
        nsa = wsNSA.Range(wsNSA.Cells(1, col.Column), wsNSA.Cells(LR, col.Column))

        num_linha = Application.Match(True, Application.Index(Application.IsNumber(nsa), 0), 0)
        nsa = wsNSA.Range(wsNSA.Cells(num_linha, col.Column), wsNSA.Cells(LR, col.Column))

    'The range starts in the column B in the worksheet, so the matrix ubound is 'col.column -1
        ReDim Preserve matrix(1 To col.Column - 1)
         matrix(col.Column - 1) = nsa

        Next

        wsSA.Range(wsSA.Cells(3, 2), wsSA.Cells(LR, LC)) = matrix


        End Sub

2 个答案:

答案 0 :(得分:1)

您可以复制全部并删除空白单元格后:

counter([a, b, c], C)

--->  counter([_ | [b, c]], C = COUNTER(1))

      ---> counter([_ | [c]], COUNTER1(1) = COUNTER(2))

           ---> counter([_ | []], COUNTER1(2) = COUNTER(3))

                ---> counter([], COUNTER1(3) = 0)

           ---> COUNTER(3) is COUNTER1(3) + 1 = 0 + 1 = 1

      ---> COUNTER(2) is COUNTER1(2) + 1 = COUNTER(3) + 1 = 1 + 1 = 2

 ---> C = COUNTER(1) is COUNTER1(1) + 1 = COUNTER(2) + 1 = 2 + 1 = 3

答案 1 :(得分:0)

如果您愿意忘记输出不应写入循环内的要求,则以下代码可能会执行您要执行的操作:

Sub MyMatrix()

    Dim wb1 As Workbook
    Set wb1 = ActiveWorkbook

    Dim wsNSA As Worksheet
    Set wsNSA = wb1.Worksheets("NSA")

    Dim wsSA As Worksheet
    Set wsSA = wb1.Worksheets("SA")

    Dim c As Long
    Dim LC As Long
    Dim LR As Long
    Dim num_linha As Long
    Dim nsa As Variant

    With wsNSA
        'LR is the Last row and LC is the last column with data
        '???? Is data1_linha declared anywhere and assigned a value? ????
        LR = .Cells(data1_linha, 1).End(xlDown).Row
        LC = .Cells(LR, 1).End(xlToRight).Column

        'Loops through columns and finds the row where numeric data begins
        For c = 2 To LC
            nsa = .Range(.Cells(1, c), .Cells(LR, c))
            num_linha = Application.Match(True, Application.Index(Application.IsNumber(nsa), 0), 0)
            wsSA.Cells(3, c).Resize(LR - num_linha + 1, 1).Value = .Range(.Cells(num_linha, c), .Cells(LR, c)).Value
        Next
    End With

End Sub