找到条件的所有组合,以在Excel

时间:2018-03-19 03:42:47

标签: sql excel-vba vba excel

我的数字从1到59,以及它们的频率。 有4个pririotes:

  1. 任意两个数字总和为60,不能超过其频率
  2. 任意三个数字总和为60,不能超过其频率
  3. 任意四个数字总和为60,不能超过其频率
  4. 任意五个数字总和为60,不能超过其频率
  5. VBA可以解决这个问题吗?有人可以帮忙吗?感谢〜

    以下是原始数据:

      
        
    • 电话:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45   46 47 48 49 50 51 52 53 54 55 56 57 58 59-频率:866 356 292 229   145 125 100 100 74 69 57 60 54 25 35 32 25 28 20 33 18 17 21 19 21 23   19 21 9 13 9 13 11 9 13 7 10 14 10 17 3 4 6 5 5 8 9 10 5 6 5 4 2 5 6 5   3 3 3
    •   

1 个答案:

答案 0 :(得分:0)

这样做你想要的吗?

enter image description here

代码:

Sub FindSeries()

Dim StartRng As Range
Dim EndRng As Range
Dim Answer As Long
Dim TestTotal As Long

Answer = Range("B1") '<<< CHANGE

Set StartRng = Range("A1")
Set EndRng = StartRng
Do Until False
    TestTotal = Application.Sum(Range(StartRng, EndRng))
    If TestTotal = Answer Then
        Range(StartRng, EndRng).Select
        Exit Do
    ElseIf TestTotal > Answer Then
        Set StartRng = StartRng(2, 1)
        Set EndRng = StartRng
    Else
        Set EndRng = EndRng(2, 1)
        If EndRng.Value = vbNullString Then
            MsgBox "No series found"
            Exit Do
        End If
    End If
Loop
End Sub