pingpongplaya10
New Member
- Joined
- Aug 21, 2011
- Messages
- 8
Hello all,
I wrote the sub routine below (it works) but am interested in making it more streamlined and dynamic with the use of a function and passing a range of cells to calculate a sum. Can someone please provide some suggestions on how to make this happen?
The way it works is that it will automatically enter a formula which sums the numbers above the selected cell. For example, if there are numbers in cells A1 through A5, clicking on A6 would call the sub and enter a formula into A6 to sum the five cells above it.
Thanks in advance.
By the way, I'm new to Excel VBA, so if anybody has any suggestions on "cleaning" up my code, please let me know.
I wrote the sub routine below (it works) but am interested in making it more streamlined and dynamic with the use of a function and passing a range of cells to calculate a sum. Can someone please provide some suggestions on how to make this happen?
The way it works is that it will automatically enter a formula which sums the numbers above the selected cell. For example, if there are numbers in cells A1 through A5, clicking on A6 would call the sub and enter a formula into A6 to sum the five cells above it.
Thanks in advance.
By the way, I'm new to Excel VBA, so if anybody has any suggestions on "cleaning" up my code, please let me know.
Code:
Private Sub SumCells() Dim EndCell As String Dim StartCell As String, StartCell2 As String StartCell = ActiveCell.Address StartCell2 = ActiveCell.Offset(-1, 0).Address Range(ActiveCell.Address, Selection.End(xlUp)).Select EndCell = Selection.End(xlUp).Address Range(StartCell).Value = "=sum(" & EndCell & ":" & StartCell2 & ")" Range(StartCell).Select End Sub
</pre>