VBA Subtraction Formula in new column

Lepley, Nicole 1 Reputation point
2022-12-15T17:20:51.173+00:00

I am trying to have columns "BP" and "EE" with a subtraction formula, "BP" = "BO"-"BN" and "EE"= "ED"-"EC". I have the below code that isn't working. Any help is greatly appreciated!

Private Sub Worksheet_Change(ByVal Target As Range)
Const RoughIn = 37
Const Drywall = 56
Const Trim = 71
Const Tile_TrimOuts = 91
Const PunchOut = 112
Const Completion = 116
Const Closing = 129

Dim rng As Range  
Dim dtm As Date  
Dim r As Long  
  
If Not Intersect(Range("E2:E" & Rows.Count), Target) Is Nothing Then  
    Application.ScreenUpdating = False  
    Application.EnableEvents = False  
    For Each rng In Intersect(Range("E2:E" & Rows.Count), Target)  
        r = rng.Row  
        If rng.Value = "" Or Not IsDate(rng.Value) Or rng.Offset(0, -1).Value = "" Then  
            Range("AK" & r).ClearContents  
            Range("BG" & r).ClearContents  
            Range("BN" & r).ClearContents  
            Range("BP" & r).ClearContents  
            Range("CG" & r).ClearContents  
            Range("DO" & r).ClearContents  
            Range("EC" & r).ClearContents  
            Range("EE" & r).ClearContents  
            Range("EG" & r).ClearContents  
        Else  
            dtm = rng.Value  
            Range("AK" & r).Value = dtm + RoughIn  
            Range("BG" & r).Value = dtm + Drywall  
            Range("BN" & r).Value = dtm + Trim  
            Range("BP" & r).Formula = "=(('BO' & r)-('BN' & r))"  
            Range("CG" & r).Value = dtm + Tile_TrimOuts  
            Range("DO" & r).Value = dtm + PunchOut  
            Range("EC" & r).Value = dtm + Completion  
            Range("EE" & r).Formula = "=(('ED' & r)-('EC' & r))"  
            Range("EG" & r).Value = dtm + Closing  
        End If  
    Next rng  
    Application.EnableEvents = True  
    Application.ScreenUpdating = True  

End If

End Sub

Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.9K Reputation points
    2022-12-15T19:38:37.64+00:00

    Check this:

    Range("BP" & r).Formula = "=BO" & r & "-BN" & r  
    Range("EE" & r).Formula = "=ED" & r & "-EC" & r  
    

  2. Lepley, Nicole 1 Reputation point
    2022-12-19T17:01:47.38+00:00

    yes it works and populates the formula in the cell.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.