使用VBA优化大型数据集(求解器不起作用)

发布于 2025-02-12 11:02:01 字数 301 浏览 1 评论 0原文

我有大量数据,需要在这些数据上获得一组到达所需数字的值。更确切地说,我拥有每张ID号的超市门票以及向客户收取的金额。

我必须拿起将添加到特定号码的门票(假设1,000,000美元)。我尝试使用求解器,其中在一列中您使用二进制1/0变量,然后只需sumif即可使求解器选择的目标值累加到目标值1M。但是,当样本很小时,这起作用。我刚刚收到的文件有超过280,000张票,因此求解器无法处理该数量的二进制变量。

如何在一列中获得总计1,000,000票的门票数量?我尝试使用“ while”循环,但我的计算机在5分钟左右后崩溃。有什么想法吗?

谢谢!

I have a large set of data on which I need to obtain a set of values that reach to a desired number. To be more precise, I have supermarket tickets each of them with one ID number and the amount that was charged to the customer.

I have to pick up tickets that add to a specific number (let's say 1,000,000 USD). I tried using solver where in one column you use a binary 1/0 variable and then just sumif to get the ones that solver selects that add up to the target value of 1M. However, this works when the sample is small. The file i just got has over 280,000 tickets, so there is no way solver can handle that number of binary variables.

How can I get in one column the number of tickets that add up to 1,000,000? I tried using a "while" loop but my computer crashes after 5 min or so. Any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2025-02-19 11:02:01

这是你想要的吗?在A1:A11中输入逻辑。

=RANDBETWEEN(1,50)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A1)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A2)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A3)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A4)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A5)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A6)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A7)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A8)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A9)),0)
=SUM(A1:A10)

结果

或,也许是这样。在A1:A41

8 中输入此内容
6
3
2
6
10
9
4
12
8
6
1
8
10
8
14
10
9
12
12
14
6
4
3
4
4
4
0
6
10
4
9
6
3
11
12
10
7
12
8
8

将92放在单元B1中,并运行此VBA。

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

Is this what you want? Enter the logic in A1:A11.

=RANDBETWEEN(1,50)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A1)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A2)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A3)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A4)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A5)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A6)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A7)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A8)),0)
=IFERROR(RANDBETWEEN(1,100-SUM($A$1:A9)),0)
=SUM(A1:A10)

Result

enter image description here

Or, maybe this. Enter this in A1:A41

8
6
3
2
6
10
9
4
12
8
6
1
8
10
8
14
10
9
12
12
14
6
4
3
4
4
4
0
6
10
4
9
6
3
11
12
10
7
12
8
8

Put 92 in cell B1, and run this VBA.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文