vba输出2D数组到工作表问题

时间:2015-07-13 14:46:25

标签: arrays excel-vba vba excel

我试图将以下2D数组输出到工作簿中的工作表。代码运行但数组在工作表上不可见。请帮我找错。感谢

Dim R As Long, C As Long, vArr As Variant

vArr = Array(Array("N", "c.181C > a", "p.Q61K", "", "0.11"), _
              Array("C", "c.98C > a", "p.S33Y", "", "36%"), _
              Array("K", "c.2447A > T", "p.D816V", "", "8"), _
              Array("B", "c.1799T > T", "p.V600E", "", "0.08"), _
              Array("N", "c.181C > a", "p.Q61K", "", "0.11"), _
              Array("C", "c.98C > a", "p.S33Y", "", "36"), _
              Array("N", "c.181C > a", "p.Q61K", "", "0.11"), _
              Array("C", "c.98C > a", "p.S33Y", "", "36"))

  For R = LBound(vArr) To UBound(vArr)
    For C = LBound(vArr(LBound(vArr))) To UBound(vArr(UBound(vArr)))
      'MsgBox "vArr(" & R & ")(" & C & ") = " & vArr(R)(C)
    Next
  Next
LRow = Worksheets("Sheet2").Range("A" & Rows.count).End(xlUp).Row

Sheets("Sheet2").Select
Worksheets("Sheet2").Range("C" & LRow + 1 & ":G" & LRow + 9).Value = vArr

1 个答案:

答案 0 :(得分:0)

有一件事你试图将8行的东西粘贴到9行的范围内。但是 - 更基本的问题是在VBA中,数组数组与2维数组不同。你应该在粘贴前转换为二维数组:

Sub test()

    Dim R As Long, C As Long, vArr As Variant
    Dim vArr2 As Variant
    Dim LRow As Long

    vArr = Array(Array("N", "c.181C > a", "p.Q61K", "", "0.11"), _
                  Array("C", "c.98C > a", "p.S33Y", "", "36%"), _
                  Array("K", "c.2447A > T", "p.D816V", "", "8"), _
                  Array("B", "c.1799T > T", "p.V600E", "", "0.08"), _
                  Array("N", "c.181C > a", "p.Q61K", "", "0.11"), _
                  Array("C", "c.98C > a", "p.S33Y", "", "36"), _
                  Array("N", "c.181C > a", "p.Q61K", "", "0.11"), _
                  Array("C", "c.98C > a", "p.S33Y", "", "36"))

    ReDim vArr2(0 To 7, 0 To 4)
    For R = 0 To 7
        For C = 0 To 4
            vArr2(R, C) = vArr(R)(C)
        Next C
    Next R

    LRow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

    Sheets("Sheet2").Select
    Worksheets("Sheet2").Range("C" & LRow + 1 & ":G" & LRow + 8).Value = vArr2

End Sub