Add sum formula with an offset from selected cell - vba

TitoElan

New Member
Joined
Jun 10, 2022
Messages
24
Office Version
  1. 365
Platform
  1. Windows
I have a code for adding group of rows from another Worksheet. The number of rows is always different. Under that group I will add some more.
When I run the code I get something like this. When I run the code the active.cell is always the one where I want to add the sum formula.
Unbenannt.png


The number of rows in the middle are always different. The number of them is set with a variable "Dim nbr As Long".
Somehow (with an offset?) I want to add a sum formula in the active cell. In that case the sum formula should sum the range P2:P7.

I would be very grateful for some help
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Do you mean something like this ?

VBA Code:
Sub InsertSum()

    Dim ws As Worksheet
    Dim rowActive As Long, colActive As Long
    Dim rowTop
    
    Set ws = ActiveSheet
    
    rowActive = ActiveCell.Row
    colActive = ActiveCell.Column   ' <-- Use this if you want to use it in any column
    rowTop = ws.Cells(rowActive - 1, colActive - 1).End(xlUp).Row
    
    With ws
        ActiveCell.Formula = "=Sum(" & .Cells(rowTop, colActive - 1).Address & ":" & _
                                        .Cells(rowActive - 1, colActive - 1).Address & ")"
    End With

End Sub
 
Upvote 0
Solution
Do you mean something like this ?

VBA Code:
Sub InsertSum()

    Dim ws As Worksheet
    Dim rowActive As Long, colActive As Long
    Dim rowTop
   
    Set ws = ActiveSheet
   
    rowActive = ActiveCell.Row
    colActive = ActiveCell.Column   ' <-- Use this if you want to use it in any column
    rowTop = ws.Cells(rowActive - 1, colActive - 1).End(xlUp).Row
   
    With ws
        ActiveCell.Formula = "=Sum(" & .Cells(rowTop, colActive - 1).Address & ":" & _
                                        .Cells(rowActive - 1, colActive - 1).Address & ")"
    End With

End Sub
yes! Thanks!
 
Upvote 0
You're welcome.
One suggestion, if you know the cell on the total line of the amounts you are summing is always going to be empty, if you change the Sum to include that cell then if you insert a row before the total it will automatically get included in the sum.
Just change rowActive - 1 to rowActive
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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