Excel VBA函数将数组打印到工作簿中 [英] Excel VBA function to print an array to the workbook
本文介绍了Excel VBA函数将数组打印到工作簿中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更优雅的方式这样做?
Sub PrintArray(Data,SheetName,StartRow,StartCol)
Dim Row As整数
Dim Col As Integer
Row = StartRow
对于i = LBound(数据,1)到UBound(数据,1)
Col = StartCol
对于j = LBound(数据,2)到UBound(数据,2)
表(SheetName).Cells(Row,Col).Value = Data(i,j)
Col = Col + 1
下一步j
行=行+ 1
下一个i
End Sub
子测试)
Dim MyArray(1到3,1到3)
MyArray(1,1)= 24
MyArray(1,2)= 21
MyArray 1,3)= 253674
MyArray(2,1)=3/11/1999
MyArray(2,2)= 6.777777777
MyArray(2,3)=Test
MyArray(3,1)= 1345
MyArray(3,2)= 4245 6
MyArray(3,3)= 60
PrintArray MyArray,Sheet1,1,$ 1
End Sub
解决方案
与其他答案相同的主题,保持简单
Sub PrintArray(Data As Variant,Cl As Range)
Cl.Resize(UBound(Data,1),UBound(Data,2))=
End Sub
Sub Test()
Dim MyArray()As Variant
ReDim MyArray(1 To 3,1 To 3 )'使它变得灵活
'填充数组
'...
PrintArray MyArray,ActiveWorkbook.Worksheets(Sheet1)。[A1]
End Sub
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?
Sub PrintArray(Data, SheetName, StartRow, StartCol)
Dim Row As Integer
Dim Col As Integer
Row = StartRow
For i = LBound(Data, 1) To UBound(Data, 1)
Col = StartCol
For j = LBound(Data, 2) To UBound(Data, 2)
Sheets(SheetName).Cells(Row, Col).Value = Data(i, j)
Col = Col + 1
Next j
Row = Row + 1
Next i
End Sub
Sub Test()
Dim MyArray(1 To 3, 1 To 3)
MyArray(1, 1) = 24
MyArray(1, 2) = 21
MyArray(1, 3) = 253674
MyArray(2, 1) = "3/11/1999"
MyArray(2, 2) = 6.777777777
MyArray(2, 3) = "Test"
MyArray(3, 1) = 1345
MyArray(3, 2) = 42456
MyArray(3, 3) = 60
PrintArray MyArray, "Sheet1", 1, 1
End Sub
解决方案
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
这篇关于Excel VBA函数将数组打印到工作簿中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文