Code:
Dim contractmonth As Variant
Dim px1 As Variant
Dim px2 As Variant
Dim i As Variant
Range("firstpay").Value = contractmonth
Range("firstpay").Offset(0, 1).Value = px1
Range("firstpay").Offset(0, 1).Activate
For i = 0 To WorksheetFunction.CountA(Range("froncontract", _
Range("frontcontract").End(xlDown))) Step 1
If WorksheetFunction.EDate(contractmonth, i + 1) < ActiveCell.Offset(i + 1, 0).Value Then
px2 = px1 + ActiveCell.Value
px2 = px1
Else: Next i
px2 = px1 + ActiveCell.Offset(i, 0).Value
px2 = px1
ActiveCell.Offset(i, 0).Activate
contractmonth = ActiveCell.Value
End If
I will preface this by saying I am a very, very green VBA user. This is a complicated loop with (I'm sure) many problems. I've summarized my intentions and observations below:
-First, it's apparent that you can't have an IF inside of FOR loop - what's the solution to this?
-The CountA function in the loop is simply serving to have the loop stop when it reaches the last cell in the column - it works.
-The contract months occur on uneven months - so if next month is less than the next contract in the future, the code should stick with the previous months contract.
-At the end of each month, if the code does not move on to the next month's contract, it will add the value from the previous month's contract to the existing price. That's why I need a second price variable (px2), so that px1 can reset to the new value (i.e. all of the previous month's contracts added to the new contract).
-If next month is greater than the next month's contract date, the code should roll into the next contract. This will reset the contractmonth to the ActiveCell, allowing the code to repeat.
Any advice would be infinitely appreciated. As a green user, this is really perplexing to me. I think my biggest issue is not being able to use IFs inside of FOR loops. Thanks so much for any insight!