0

我试图让代码从输入框中读取一个值,这是所需的投资金额,即。3000. 然后读取以特定价格可用​​的 btc 数量的列表(40 行长),并连续总结这些总美元金额(数量*价格),直到添加下一行的点大于所需的投资金额(即,我正在尝试以最便宜的方式获得一堆 btc)。

然后我会写一点来弥补下一行的其余值,因为它显然不会完美地达到,但我似乎无法让这一点工作。当我执行代码时,我得到了一些没有多大意义的奇怪结果。我已经放了一个表格的例子,所以你可以看到我正在使用什么(第一个价格 94.25 是 B3/ActiveCell) 这可能非常微不足道,但我以前从未做过任何这些事情。感谢您抽出宝贵时间,我希望我已经足够清楚地概述了它。

Sub Projected()
Dim InvestValue As Single
Dim SumBTCE As Single
Dim Sumup As Single
Dim NumBTC As Single
Dim Count As Integer

InvestValue = InputBox("Input investment amount:")
NumBTC = 0
Sumup = 0
ActiveWorkbook.Sheets("BTC-E Data").Cells(3, 2).Select

Do Until (Sumup + (ActiveCell.Offset(Count, 0).Value * ActiveCell.Offset(Count, 1).Value)) >= InvestValue
    For Count = 1 To 40
        Sumup = Sumup + ActiveCell.Offset(Count - 1, 0).Value * ActiveCell.Offset(Count - 1, 1).Value
        NumBTC = NumBTC + ActiveCell.Offset(0, 1).Value
    Next Count
Loop
MsgBox NumBTC
MsgBox Sumup
End Sub

price   BTC USD
94.25   0.1 9.425
94.439  0.34583324  32.66014535
94.44   2   188.88
94.443  0.011   1.038873
94.444  0.4 37.7776
94.493  0.025   2.362325
94.5    0.1 9.45
94.55   0.1 9.455
94.6    0.1 9.46
94.601  0.5 47.3005
94.648  0.0112  1.0600576
94.649  4.12801098  390.7121112
94.65   35.75926753 3384.614672
94.664  2.128011    201.4460333
94.665  3.5 331.3275
94.679  0.1395  13.2077205
94.68   0.15    14.202
94.689  2.128011    201.4992336
94.69   18.73708352 1774.214439
94.698  0.010978    1.03959464
94.699  0.093   8.807007
94.7    0.1 9.47
94.704  0.025   2.3676
94.736  0.0837  7.9294032
94.737  0.09    8.52633
94.749  2.128011    201.6269142
94.75   20.1    1904.475
94.755  0.1 9.4755
94.8    0.1 9.48
94.801  0.03758691  3.56327665
94.81   5.7236763   542.66175
94.829  0.15    14.22435
94.84   0.20095058  19.058153
94.85   0.1 9.485
94.87   0.01    0.9487
94.879  0.401   38.046479
94.88   0.01    0.9488
94.887  0.40930425  38.83765236
94.89   0.01    0.9489
94.9    0.30106377  28.57095176
4

2 回答 2

1

这是我的做法:

A2 包含您的目标,例如 $3000。C2:E41 包含您的数据 F2 公式:

=SUMPRODUCT((C$2:C2*D$2:D2))

G2公式:

=SUM(F$2:F2)>=$A$2

H2公式:

=IF(G2,MAX(0,$A$2-SUM(F$1:F1)),D2)

然后将公式复制下来。

您可以组合这些公式,但这样更容易遵循。

在此处输入图像描述

于 2013-08-10T02:18:01.140 回答
0

试试下面的代码

Sub Projected()
Dim InvestValue, SumBTCE, Sumup, NumBTC As Single
Dim Count, LastRow, BTC, Price As Integer

InvestValue = InputBox("Input investment amount:")
NumBTC = 0
Sumup = 0

With Worksheets("BTC-E Data")
    LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row 'finding the last row in column 2
End With

For Count = 3 To LastRow
    Price = ActiveWorkbook.Sheets("BTC-E Data").Range("B" & Count).Value
    BTC = ActiveWorkbook.Sheets("BTC-E Data").Range("C" & Count).Value
    Sumup = Sumup + (Price * BTC)
    NumBTC = NumBTC + BTC
    If Sumup >= InvestValue Then Exit For
Next

MsgBox NumBTC
MsgBox Sumup
End Sub

上述代码的输出

数字比特币 = 100.2461

总结 = 9520.399976

于 2013-08-10T09:40:26.940 回答