'Clear a cell in Excel after contents used in formula

I am trying to create a scenario where if I am to enter a number into a cell, I want that number to be used in another cells formula and than the cell I entered the number in to be cleared after hitting enter.

Example: Cell G2 has formula =Sum(A2,B2)-C2

Cell B2 is 10

Cell C2 is 2

Enter 7 into cell A2

After hitting enter Cell G2 should show 15 and Cell A2 should be cleared.

So far I am only able to get the cell to clear:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
        Target.ClearContents
    Application.EnableEvents = True
End Sub

I am trying to use Application.Evaluate("Sum(A2, B2) - C2") for the formula but I cannot get them to work together.

Is this possible?

Thank you for any assistance.



Solution 1:[1]

A Worksheet Change: Evaluate a Formula

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const FirstCellAddress As String = "A2"
    Const FormulaColumn As String = "G"
    
    Dim irg As Range
    With Me.Range(FirstCellAddress)
        Set irg = Intersect(.Resize(Me.Rows.Count - .Row + 1), Target)
    End With
    If irg Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    
    With irg
        Intersect(.EntireRow, Me.Columns(FormulaColumn)).Value _
            = Me.Evaluate(.Cells(1).Address & "+" & .Cells(1) _
            .Offset(, 1).Address & "-" & .Cells(1).Offset(, 2).Address)
        .ClearContents
    End With
    
    Application.EnableEvents = True

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VBasic2008