excel vba - Trying to get vba to "loop until" with a count -
i'm trying code read value input box desired investment amount ie. 3000. read down list (40 rows long) of amount of btc available @ particular price, , consecutively sum these total dollar amounts (quantity*price) going down list until point adding next line greater desired investment amount (ie. i'm trying see cheapest way acquire bunch of btc).
i'll write bit make make rest of value next line won't reached can't seem bit work. when execute code i'm getting weird results don't make sense. i've put example of table in can see i'm working (the first price 94.25 b3/activecell) extremely trivial i've never done of stuff before. time , hope i've outlined enough.
sub projected() dim investvalue single dim sumbtce single dim sumup single dim numbtc single dim count integer investvalue = inputbox("input investment amount:") numbtc = 0 sumup = 0 activeworkbook.sheets("btc-e data").cells(3, 2).select until (sumup + (activecell.offset(count, 0).value * activecell.offset(count, 1).value)) >= investvalue count = 1 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
here's how i'd it:
a2 contains goal, e.g., $3000. c2:e41 contains data f2 formula:
=sumproduct((c$2:c2*d$2:d2))
g2 formula:
=sum(f$2:f2)>=$a$2
h2 formula:
=if(g2,max(0,$a$2-sum(f$1:f1)),d2)
then copy formulas down.
you combine these formulas, it's easier follow way.
Comments
Post a Comment