vba:将范围分配给数组,然后乘以该范围

时间:2014-02-06 10:45:27

标签: excel excel-vba vba

Private Sub CommandButton1_Click()
    Dim ary(2, 2) As Variant

    For p = 0 To 2
        For q = 0 To 2
            ary(p, q).Value = Range("a1:c3")
        Next q
    Next p

    For f = 0 To 2
        For t = 0 To 2
            Cells(f + 1, t + 5).Value = ary(f, t) * 2
        Next t
    Next f
End Sub

我正在编写上面的程序来为数组分配一个范围。但它显示一个称为对象所需的错误。当我尝试调试时,选择语句

ary(p,q).Value =范围(“a1:c3”)

可以帮助一些人吗?

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButton1_Click()
    Dim ary As Variant
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ActiveWorkbook
    Set ws = wb.Sheets(1)

    ary = ws.Range("a1:c3")

    For f = 1 To UBound(ary)
        For t = 1 To UBound(ary)
            Cells(f + 1, t + 5).Value = ary(f, t) * 2
        Next t
    Next f
End Sub