Option Explicit Const NUM As Long = 9 Sub abc1() Dim i, j ReDim a(1 To NUM, 1 To NUM) For i = 1 To NUM For j = 1 To i a(i, j) = j Next Next Call putdata(a) End Sub Sub abc2() Dim i, j, p, n ReDim a(1 To NUM, 1 To NUM + 1) p = NUM \ 2 + 1 For i = 1 To NUM For j = p - (i \ 2) To i + p - (i \ 2) - 1 If j >= p And i Mod 2 = 0 Then n = 1 Else n = 0 a(i, j + n) = j - (p - (i \ 2)) + 1 Next Next Call putdata(a) End Sub Function putdata(a) Cells.Clear [a1].Resize(UBound(a), UBound(a, 2)) = a Columns.AutoFit End Function
'杨辉三角形 Option Explicit Const NUM As Long = 9 Sub abc3() Dim a, i, j, p ReDim a(1 To NUM, 1 To 2 * NUM - 1) For i = 1 To NUM a(i, NUM - p) = 1: a(i, NUM + p) = 1 If i > 2 Then For j = NUM - p + 2 To NUM + p - 2 Step 2 a(i, j) = a(i - 1, j - 1) + a(i - 1, j + 1) Next End If p = p + 1 Next Call putdata(a) End Sub Function putdata(a) Cells.Clear [a1].Resize(UBound(a), UBound(a, 2)) = a Columns.AutoFit End Function