以下為2種常見的一維陣列對儲存格填值方式。
第一種:陣列無指定起始
Sub 陣列無指定起始() Dim a(3) As Integer, i As Integer For i = 1 To 3 a(i) = i Next Sheets(1).Cells.Clear Sheets(1).Cells(1, 1).Resize(, UBound(a) + 1).Value = a Sheets(1).Cells(3, 1).Resize(UBound(a) + 1).Value = Application.Transpose(a) End Sub執行畫面與結果:
第二種:陣列有指定起始
Sub 陣列有指定起始() Dim a(1 To 3) As Integer, i As Integer For i = 1 To 3 a(i) = i Next Sheets(1).Cells.Clear Sheets(1).Cells(1, 1).Resize(, UBound(a) + 1).Value = a Sheets(1).Cells(3, 1).Resize(UBound(a) + 1).Value = Application.Transpose(a) End Sub執行畫面與結果:
以下為3種常見的二維陣列對儲存格填值方式。
第一種:陣列宣告,Row與Column起始皆未指定
Sub 陣列Row與Column起始皆未指定() Dim a(2, 3) As Integer, i As Integer, j As Integer For i = 1 To 2 For j = 1 To 3 a(i, j) = i * j Next Next Sheets(1).Cells.Clear Sheets(1).Cells(1, 1).Resize(UBound(a) + 1, UBound(a, 2) + 1).Value = a End Sub執行畫面與結果:
第二種:陣列宣告,Row起始指定1與Column起始未指定
Sub 陣列Row起始指定1與Column起始未指定() Dim a(1 To 2, 3) As Integer, i As Integer, j As Integer For i = 1 To 2 For j = 1 To 3 a(i, j) = i * j Next Next Sheets(1).Cells.Clear Sheets(1).Cells(1, 1).Resize(UBound(a) + 1, UBound(a, 2) + 1).Value = a End Sub執行畫面與結果:
第三種:陣列宣告,Row起始指定1與Column起始指定1
Sub 陣列Row起始指定1與Column起始指定1() Dim a(1 To 2, 1 To 3) As Integer, i As Integer, j As Integer For i = 1 To 2 For j = 1 To 3 a(i, j) = i * j Next Next Sheets(1).Cells.Clear Sheets(1).Cells(1, 1).Resize(UBound(a) + 1, UBound(a, 2) + 1).Value = a End Sub執行畫面與結果:
以上陣列對儲存格填值方式,請注意陣列宣告方式不同,其他部分相同,如此將導致不同的果,各位可以自行偵錯查看其中的變化。
檔案連結。
沒有留言:
張貼留言