如何输入数组中行数的值?



我想输入帕斯卡三角形中的行数,该行将在Dim i(10, 10) As Integer行中使用。

下面是没有此函数的代码:

Sub PascalTriangle()
Dim i(10, 10) As Integer
Dim p, j As Integer
i(0, 0) = 1
i(1, 0) = 1
i(1, 1) = 1
For p = 2 To 10 'put one for the first column, here the number of row is 10
i(p, 0) = 1
For j = 1 To p
i(p, j) = i(p - 1, j - 1) + i(p - 1, j)
Next
Next
For p = 0 To 10
For j = 0 To p
Cells(p + 1, j + 1) = i(p, j)
Next
Next
End Sub

试试这个,请:

Sub PascalTriangle_ChooseNoOfRows()
Dim i() As Long, p As Long, j As Long, lngR As Long, inpAns As String
inpAns = InputBox("Now much rows must Pascal function process?", "Choose the number of rows", 10)
If inpAns = "" Then Exit Sub
If IsNumeric(inpAns) Then
lngR = CLng(inpAns)
Else
MsgBox "You must write a number!": Exit Sub
End If
ActiveSheet.Cells.Clear
ReDim i(lngR, lngR)
i(0, 0) = 1: i(1, 0) = 1: i(1, 1) = 1
For p = 2 To lngR
i(p, 0) = 1
For j = 1 To p
i(p, j) = i(p - 1, j - 1) + i(p - 1, j)
Next j
Next
For p = 0 To lngR
For j = 0 To p
Cells(p + 1, j + 1) = i(p, j)
Next j
Next
End Sub

相关内容

  • 没有找到相关文章

最新更新