formula sum in last known column

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
This is trickier than I thought.

In this example cell H3 would be =sum(B3:G3), but I don't always know what the last column will be.

Copy of Area 84 IND Sept-October.xlsm
BCDEFGH
1
2600336A3600336PL600336SSIFU600336SSIVD600336VIQS601036FTotal
3857306
4110100
5220100
6420000
7120101
8130000
9100111
10043112
11020000
12006001
13001021
14000101
15000200
report
Cell Formulas
RangeFormula
B3:G15B3=COUNTIFS(data!$H:$H,$A3,data!$A:$A,B$2)
Named Ranges
NameRefers ToCells
data!_FilterDatabase=data!$A$3:$AI$3B3:G15


I tried the following, I think it's close.

VBA Code:
    Dim Lcol As Long
    Dim rng As Range
    Dim rng2 As Range
   
    Lcol = Cells(2, Columns.Count).End(xlToLeft).Column
   
    Set rng = Range("A2")
    Set rng2 = rng.Offset(1, Lcol - 1)

    rng.Offset(0, Lcol).Value = "Total"
    rng.Offset(1, Lcol).Value = "=sum(B3:" & rng2 & ")"

If I simply use rng2.select G3 is selected. I think something with my formula "=sum(B3:" & rng2 & ")" isn't right.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
VBA Code:
    rng.Offset(1, Lcol).FormulaR1C1 = "=sum(rc2:rc[-1])"
 
Upvote 0
Solution
How about
VBA Code:
    rng.Offset(1, Lcol).FormulaR1C1 = "=sum(rc2:rc[-1])"
I tried
VBA Code:
    Dim rng As Range
    Set rng = Range("A2")
    rng.Offset(1, Lcol).FormulaR1C1 = "=sum(rc2:rc[-1])"
What it's doing is selecting columns B:G

Hold please, I ran it again and now it's giving me a circular reference. I'm investigating.
 
Upvote 0
How about
VBA Code:
    rng.Offset(1, Lcol).FormulaR1C1 = "=sum(rc2:rc[-1])"
Thanks Fluff, one column off but I figured it out.

VBA Code:
    Dim Lcol As Long
    Dim rng As Range

    Lcol = Cells(2, Columns.Count).End(xlToLeft).Column
   
    Set rng = Range("A2")
    rng.Offset(1, Lcol - 1).FormulaR1C1 = "=sum(rc2:rc[-1])"

Using FormulaR1C1 is a first for me. :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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