I have there the sample data of my project. As you can see there are 4 colums: deadline, submitted, description, & days_delayed. In addition, in the same sheet I have the insertion form of data. Where Range G1 is the deadline and the G3 is the actual date when the user submits its document.
In this code you'll the conditional statement written in formula way inside of vba. So that every time adds new row the formula will follows and copied to its corresponding cell range.
Private Sub CommandButton1_Click()
Dim Deadline, Submitted, Description, Days_Delayed As String
Worksheets("Sheet3").Select
Deadline = Range("G1")
Submitted = Range("G3")
Description = "=IF(AND(D2>0,ISBLANK(B2)),"NO-
DOCUMENT",IF(AND(D2<=0,NOT(ISBLANK(B2))),"ON-TIME", IF(AND(D2 > 0,NOT(ISBLANK(B2))),"DELAYED")))"
Days_Delayed = "=IF(COUNT(A2:B2)=2,B2-A2,IF(B2="","0"))"
Worksheets("Sheet3").Select
Worksheets("Sheet3").Range("A2").Select
If Worksheets("Sheet3").Range("A2").Offset(1, 0) <> "" Then
Worksheets("Sheet3").Range("A2").End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = Deadline
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Submitted
ActiveCell.Offset(0, 1).Select
ActiveCell.Formula = Description
ActiveCell.Offset(0, 1).Select
ActiveCell.Formula = Days_Delayed
End Sub
This code is working fine but when I tried to implement the Formula inside of the vba codes. The error say "End Of Statement"
Fixed By Peh The Insertion of formula to VBA should be like this
Days_Delayed = "=IF(COUNT(N36:O36)=2,O36-N36,IF(O36="""",TODAY()-N36))"
Another Question is:
Since the add new row is a new data and the formula should not placed a defined range inside of it.
Days_Delayed = "=IF(COUNT(N36:O36)=2,O36-N36,IF(O36="""",TODAY()-N36))"
In this formula you can see that the formula ranges are defined. What I'm trying to fix is to do something like this:
Days_Delayed = "=IF(COUNT(N(i):O(i))=2,O(i)-N(i),IF(O(i)="""",TODAY()-(i)))"
You can see that there is variable i so when the new row comes in. The formula will automatically knows what cells needed to be calculated. I guess, looping is the appropriate way for this. Because If I remain the same formula with defined ranges. The output will be same. Is that possible?