0

这就是我正在尝试做的事情:我在 (D) 列中有 StockOH,我想知道每个金额可以覆盖多少天的销售。因此,例如,如果 D2 中的 stockOH 值大于值 E2:E5 的总和但小于 Sum(E2:E6),这意味着我可以涵盖 4 天的销售。

我正在使用嵌套到 for 循环的 Do until 函数。我不确定为什么第一个结果是正确的,而下一个结果不是

在此处输入图像描述

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

 Dim y, x, Ssellout As Double

 'loop thorugh the stock values in column D

 For y = 2 To 5

 'Get each amount of stock
  Ssellout = Cells(y, 5).Value

 x = y

 'FInd the breakout point when sum of sellout is higher than stockOH
 Do Until Cells(y, 4).Value < Ssellout

 'Add 1 to the counter in case condition is not valid
  x = x + 1

 'REport value found as a breakeven
 Cells(y, 6).Value = x

'Add another day of sales to the sum of sales
  Ssellout = Ssellout + Cells(x, 5).Value
 Loop

 MsgBox (y)

 Next
End Sub
4

1 回答 1

0

我终于用 FOR Loop 和 Do While 函数进行了管理。

这是代码

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim x, y As Integer

Dim sellout, stock As Double

For y = 0 To 4

'Define counter for sum IMPORTANT it should be inside the for loop
sellout = 0
x = 2

Do While Cells(2 + y, 4).Value > sellout

sellout = sellout + Cells(x + y, 5).Value
Cells(2 + y, 6).Value = x - 2
x = x + 1
Loop

Next


End Sub
于 2020-03-23T15:08:41.357 回答