С использованием Exit For
| Код | Private Sub CommandButton1_Click() Dim d As Integer Dim c As Integer
d = UsedRange.Rows.Count c = UsedRange.Columns.Count For RwIndex = 4 To d For RwIn = 5 To c If Cells(RwIndex, RwIn).Value > 0 And Range("A" & RwIndex) > 0 Then Range("D" & RwIndex).Value = "" exit for End If Next RwIn Next RwIndex
For RwIndex = 4 To d For RwIn = 5 To c If Cells(RwIndex, RwIn).Value > 0 And Range("A" & RwIndex) > 0 Then Range("D" & RwIndex).Value = Range("D" & RwIndex).Value + (Cells(3, RwIn).Value * Cell(RwIndex, RwIn).Value)
End If
Next RwIn Next RwIndex End Sub
|
С использованием аккумулятора и один цикл
| Код | Private Sub CommandButton1_Click() Dim d As Integer Dim c As Integer Dim accumulator as Integer
d = UsedRange.Rows.Count c = UsedRange.Columns.Count For RwIndex = 4 To d For RwIn = 5 To c If Cells(RwIndex, RwIn).Value > 0 And Range("A" & RwIndex) > 0 Then Range("D" & RwIndex).Value = "" accumulator = accumulator + (Cells(3, RwIn).Value * Cell(RwIndex, RwIn).Value) End If Next RwIn if Range("D" & RwIndex).Value = "" then Range("D" & RwIndex).Value = accumulator Next RwIndex End Sub
| |