Sub kkk()
Dim theBalance&, i&, j&, k&, m&, theNum&, theNumTemp&
Dim theStr$, arr As Variant, d As Object
'
j = 2 + Int(Rnd * 30)
ReDim arr(1 To j, 1 To 1)
theBalance = 600
theNum = theBalance \ j
For i = 1 To j
arr(i, 1) = theNum
Next i
theNum = theBalance Mod j
arr(j, 1) = arr(j, 1) + theNum
'
Set d = CreateObject("SCripting.Dictionary")
Randomize
For i = 1 To j - 1
Do
theNum = Int(Rnd * arr(i, 1))
theNumTemp = arr(i, 1) - theNum
If Not d.Exists(theNumTemp) Then
arr(i, 1) = theNumTemp
d(theNumTemp) = ""
arr(i + 1, 1) = arr(i + 1, 1) + theNum
Exit Do
End If
Loop
Next i
k = j
i = 0
Do While d.Exists(arr(k, 1))
theNum = Int(Rnd * arr(k, 1))
theNumTemp = arr(k, 1) - theNum
If Not d.Exists(theNumTemp) Then
arr(k, 1) = theNumTemp
d(theNumTemp) = ""
For m = 1 To k - 1
theNumTemp = arr(m, 1) + theNum
If Not d.Exists(theNumTemp) Then
arr(m, 1) = theNumTemp
Exit Do
End If
Next m
'
For m = k + 1 To j
theNumTemp = arr(m, 1) + theNum
If Not d.Exists(theNumTemp) Then
arr(m, 1) = theNumTemp
Exit Do
End If
Next m
End If
i = i + 1
If i = 5000 Then Exit Do
Loop
'
theStr = arr(1, 1)
For i = 2 To j
theStr = theStr & " " & arr(i, 1)
Next i
MsgBox "拆分成 " & j & " 个数字:" & vbLf & vbLf & theStr
Set d = Nothing
End Sub
你没有说明是否允许生成的若干个数存在重复。