像这样的排列组合问题,可以通过构建辅助列解决,在前4列列出所有数字,第5列新建辅助列,条件为=IF(AND(SUM(A1:D1)>10,SUM(A1:D1)<25),A1&","&B1&","&C1&","&D1,"")。
如下图:
图中为模拟数据,与原题不一样
但这种方式需要20^4行数据,不是很实用。
而使用vba可以很快地解决这个问题:
Sub 排列组合()
Dim b, c, d, e, f As Integer
b = 2 '从B2开始生成
For c = 1 To 20
For d = 1 To 20
For e = 1 To 20
For f = 1 To 20
sumthis = c+d+e+f
if sumthis>10 and sumthis <25 then
Range("b" & b) = c & "," & d & "," & e & "," & f
b = b + 1 '跳转到下一单元格
Next
Next
Next
Next
Next
End Sub
复制以上代码,打开excel文件,按alt+F11键,在上方菜单中选择插入模块,粘贴该代码,按F5运行即可。