在用VBA做数据排列组合的时候想要这个组合中单个组合数据总和在一个范围内怎么才能做到
2个回答
展开全部
1-10中选择,本人理解为0-9,10为双数值,包含0,数值的基础是通过0-9来组合的。代码如下
Sub 排列组合()
For A = 0 To 9
For B = 0 To 9
For C = 0 To 9
For d = 0 To 9
p = A + B + C + d
If p >= 10 And p <= 25 Then
k = k + 1
Cells(k, 1) = A & B & C & d
End If
Next
Next
Next
Next
End Sub
注意将表格A列单元格式为自定义:0000,不然前面没有0!本列共有8288个符合总和10-25的条件!
如果您希望第一位的值是从1开始,则改 for A=1 to 9 即可!
展开全部
For intSum = 10 To 25
For A = 1 To 10
If intSum - A < 1 + 2 + 3 Then Exit For
For B = 1 To 10
If B = A Then
If B = 10 Then
Exit For
Else
B = B + 1
End If
End If
If intSum - A - B < 1 + 2 Then Exit For
For C = 1 To 10
Select Case C
Case A
If C = 10 Then
Exit For
Else
C = C + 1
If C = B Then
If C = 10 Then
Exit For
Else
C = C + 1
End If
End If
End If
Case B
If C = 10 Then
Exit For
Else
C = C + 1
If C = A Then
If C = 10 Then
Exit For
Else
C = C + 1
End If
End If
End If
End Select
D = intSum - A - B - C
If D < 1 Or D > 10 Then Exit For
If D <> A And D <> B And D <> C Then
I = I + 1
Range("A" & I) = A & "/" & B & "/" & C & "/" & D
End If
Next C
Next B
Next A
Next intSum
共2668种组合
For A = 1 To 10
If intSum - A < 1 + 2 + 3 Then Exit For
For B = 1 To 10
If B = A Then
If B = 10 Then
Exit For
Else
B = B + 1
End If
End If
If intSum - A - B < 1 + 2 Then Exit For
For C = 1 To 10
Select Case C
Case A
If C = 10 Then
Exit For
Else
C = C + 1
If C = B Then
If C = 10 Then
Exit For
Else
C = C + 1
End If
End If
End If
Case B
If C = 10 Then
Exit For
Else
C = C + 1
If C = A Then
If C = 10 Then
Exit For
Else
C = C + 1
End If
End If
End If
End Select
D = intSum - A - B - C
If D < 1 Or D > 10 Then Exit For
If D <> A And D <> B And D <> C Then
I = I + 1
Range("A" & I) = A & "/" & B & "/" & C & "/" & D
End If
Next C
Next B
Next A
Next intSum
共2668种组合
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询