
hello
To create a loop for repeating the code block 54 times, you can use a For loop. Here's an example of how you can modify your code to incorporate the loop:
In this modified code, a For loop is added to repeat the code block 54 times. The loop iterates from 1 to 54, and inside the loop, the condition check is performed using the SumOffset function. The SumOffset function calculates the cumulative sum of the offset values based on the iteration number.
Please note that in your original code, the calculation logic seemed to have a pattern where the multiplier for Cells(5, 2) increases by 1 for each additional offset. I have modified the calculation to match that pattern, but please double-check if it aligns with your desired calculation logic.
By using the For loop, you can avoid duplicating the code block 54 times, making the code easier to manage and faster to execute.
vba
Sub check10()
Dim k As Variant
Dim i As Integer
k = Cells(3, 3)
For i = 1 To 54 ' Repeat the code block 54 times
If Cells(4, 2) <= k + SumOffset(i) Then
Cells(5, 2) = (i - 1) * 5 + (Cells(4, 2) - k) / (Cells(3, 3).Offset(0, i) / 5)
Exit For ' Exit the loop once the condition is met
End If
Next i
End Sub
Function SumOffset(iteration As Integer) As Double
Dim sum As Double
Dim j As Integer
For j = 1 To iteration
sum = sum + Cells(3, 3).Offset(0, j)
Next j
SumOffset = sum
End Function