在第一个问题之后,我尝试添加另一个循环来遍历列 D 中 StockOh 的所有值。基本上之后,它找到了 D2 中第一个数字的 i,我想对 D3 中的值重复迭代, D4,...我认为问题出在使用“退出循环”的某个地方,但会看到。
这是一个附加问题,如果我应该打开一个新线程,请告诉我。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' get last row in E column
Dim lastRow As Long
lastRow = Cells(Rows.Count, 5).End(xlUp).Row
' get last row in D column
Dim lastRow_stock As Long
lastRow_stock = Cells(Rows.Count, 4).End(xlUp).Row
Dim i, stock, y As Integer
' define sum
Dim sumInE As Double
sumInE = 0
'Define counter
y = 0
'loop over D coloumn
For stock = 2 + y To lastRow_stock
'Loop over E coloumn
For i = 2 To lastRow
'Get value in coloumn D
Dim d2value As Double
d2value = Cells(stock, 4).Value
sumInE = sumInE + Cells(i, 5).Value
If sumInE > d2value Then
Cells(stock, 6).Value = i
Exit For
End If
MsgBox (y)
y = y + 1
Next
Next stock
End Sub