Excel VBA function to print an array to the workbook

后端 未结 6 1744
心在旅途
心在旅途 2020-12-10 03:17

I\'ve written a macro that takes a 2 dimensional array, and \"prints\" it to equivalent cells in an excel workbook.

Is there a more elegant way to do this?



        
6条回答
  •  执笔经年
    2020-12-10 03:59

    On the same theme as other answers, keeping it simple

    Sub PrintArray(Data As Variant, Cl As Range)
        Cl.Resize(UBound(Data, 1), UBound(Data, 2)) = Data
    End Sub
    
    
    Sub Test()
        Dim MyArray() As Variant
    
        ReDim MyArray(1 To 3, 1 To 3) ' make it flexible
    
        ' Fill array
        '  ...
    
        PrintArray MyArray, ActiveWorkbook.Worksheets("Sheet1").[A1]
    End Sub
    

提交回复
热议问题