An implementation of Visual Basic that is built into Microsoft products.
Append newcell.Address rather than newcell itself, because Address converts the Range object into formula text such as A10.
Sub UpdateFormula()
Dim ws As Worksheet
Dim newCell As Range
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set newCell = ActiveCell
If Not newCell.Worksheet Is ws Then
MsgBox "Select a cell on Sheet1 and run the macro again."
Exit Sub
End If
ws.Range("A7").Formula = ws.Range("A7").Formula & _
"+" & newCell.Address(False, False)
End Sub
With A10 active, this changes the formula to:
=A1+A2+A3-A5+A10
The two False arguments produce a relative address (A10). Without them, Address defaults to an absolute address ($A$10):
newCell.Address ' $A$10
newCell.Address(False, False) ' A10
ActiveCell returns a Range object, while Range.Address returns its reference as a string. The Formula property expects a formula expressed in A1-style notation.^1^ ^2^ Directly concatenating newCell invokes the range’s default value, which is why the formula receives 200 instead of A10.
The worksheet check is important because ActiveCell belongs to whichever worksheet is currently active.^3^ Without that check, selecting A10 on another sheet and appending only A10 would make the resulting formula refer to Sheet1!A10, not the selected cell.
References