Capt,
As an alternative, you might want to consider this. Put your totals at the
top of the sheet, e.g.: =SUM(A2:A65536). Now rows can be added and they're
included. If you freeze the row(s) containing the totals (and headings and
other pretty stuff) the totals are always visible no matter where the user
scrolls. Window - Freeze panes. SOme people get all squirmy at the idea of
having totals at the top, probably because when they were taught to add they
put the sum at the bottom, and that's the way it is. Ever shall be.
They'll come around. It works great.
If you still want to build formulas in a macro, here's some stuff:
Put one of these in a regular module:
Sub AutoSum()
'Creates =SUM() formula in cell immediately below selection. Users first
selects cells to sum
ActiveSheet.Unprotect Password:="mypass"
Cells(Selection.Row + Selection.Rows.Count, Selection.Column).Formula = _
"=SUM(" & Selection.Address & ")"
ActiveSheet.Protect Password:="mypass"
End Sub
Sub AutoSum2()
Creates =SUM() formula from active cell to top of region containing data
Dim Contents As String
ActiveSheet.Unprotect Password:="mypass"
Contents = Range(Selection.Offset(-1, 0), Selection.Offset(-1,
0).End(xlUp)).Address
ActiveCell = "=SUM(" & Contents & ")"
ActiveSheet.Protect Password:="mypass"
End Sub
Sub AutoSum3()
Creates =SUM() formula from top of worksheet to activecell
Dim Contents As String
ActiveSheet.Unprotect Password:="mypass"
Contents = Range(Selection.Offset(-1, 0), Cells(1,
Selection.Column)).Address
ActiveCell = "=SUM(" & Contents & ")"
ActiveSheet.Protect Password:="mypass"
End Sub
The user makes a selection and clicks a button you've created on a toolbar
to run the macro. It's probably a bit user-error-prone. It's a start.
You can use View - Toolbars - Customize to create toolbars and make your own
buttons, and the right-click on a button you've created to draw a face on it
and assign it to your macro.