|
本帖最后由 →友の良杰← 于 2012-11-4 14:26 编辑
代码如下:
[code=vb]
Private Sub CommandButton1_Click()
Dim i, j, k, ir, sum, arr() As Integer
Dim Str As String
Dim r, rg As Range
Set rg = Application.InputBox("请选择一个区域", "选择区域", Type:=8)
ReDim arr(rg.Count) As Integer
ir = 0
'判断数据有效性,并初始化数组
For Each r In rg
If IsNumeric(r.Value) = False Then
MsgBox ("你的选择有误,请确认选择都为数字!")
Exit Sub
End If
arr(ir) = Val(r.Value)
ir = ir + 1
Next
'调试数据
'Debug.Print (UBound(arr))
'计算表达式sum = sum + arr(i) * arr(k) * arr(j)
For i = 0 To UBound(arr) - 1
For j = i + 1 To UBound(arr) - 1
For k = i + 2 To UBound(arr) - 1
Str = Str & arr(i) & "*" & arr(j) & "*" & arr(k) & "+"
sum = sum + arr(i) * arr(k) * arr(j)
'在立即窗口输出数据
Debug.Print ("i=" & i & "[" & arr(i) & "],j=" & j & "[" & arr(j) & "],k=" & k & "[" & arr(k) & "],sum=" & sum)
Next
i = i + 1
Next
i = i + 1
Next
Str = Left(Str, Len(Str) - 1)
MsgBox Str & "=" & sum
End Sub
[/code]
|
|