Improve/Optimize code

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.

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>
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
sorry, code didn't paste correctly... reposting...

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
 
Upvote 0
Can't you just use the AutoSum button on the toolbar?
 
Upvote 0
You are correct, AutoSum performs the same exact operation. However, to try and improve at writing reusable code, I was interested in seeing other ways of re-writing this.
 
Upvote 0
select the first cell instead

ActiveCell.End(xlDown).Offset(1, 0) = "=sum(" & ActiveCell.Address & ":" & ActiveCell.End(xlDown).Address & ")"
ActiveCell.End(xlDown).Activate
 
Upvote 0
What range do you want to sum?

If you wanted to sum from the first row to the selected row:
Code:
ActiveCell.Offset(1).FormulaR1C1 = "=SUM(R1C:R[-1]C)"
That puts the formula in the cell below the active cell.
 
Upvote 0
maybe:

Code:
Sub meh()
    With ActiveCell
        .Formula = "=sum(" & Range(.Offset(-1), .Offset(-1).End(xlUp)).Address & ")"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top