I'm trying to write a macro that will place a formula under a column of numbers, such as sum or average. I came up with this:
Sub foo()
ActiveSheet.Range(ActiveCell, ActiveCell.End(xlDown)).Select
ActiveCell.End(xlDown).Offset(1) = Application.WorksheetFunction.Sum(Selection)
End Sub
This function will find the sum and place it under the last number in a column of data that you have selected the top of. That works but it is not dynamic, the new value is just a value, if I change one of the numbers in the column, the cell holding the sum does not change. So I'm trying to place the actual function in the cell. This is what I have currently:
Sub foo()
ActiveCell.Offset(1).Formula = "=Sum(ActiveCell:ActiveCell.End(xlUp))"
End Sub
This time I started at the bottom, the active cell is currently intended to be the last value in the column. No errors are detected but the output is bad, "#NAME". I tried playing with the quotes but didn't get anything. For example:
Sub foo()
ActiveCell.Offset(1).Formula = "=Sum(" & ActiveCell:ActiveCell.End(xlUp) & ")"
End Sub
Any ideas?
Address()
of the range you want to sum, and use that in the formula.ActiveSheet.Range(ActiveCell, ActiveCell.End(xlDown)).Address()
will give you the address of the range you want to sum. Substitiute that in the formula instead ofActiveCell:ActiveCell.End(xlUp)