VBA finding SUM of all cells to left of cell

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I need a VBA formula to enter in a cell in a certain row (row # = WeeklyTotalsAddRow variable) in the column with the header "Total" in row 1 that will give me the sum of all the cells to the left of this cell except columns A-C.

The column with the "Total" header could be anywhere from column G through BA.

I write to the cell with this:
Code:
Cells(WeeklyTotalsAddRow, Application.WorksheetFunction.Match("Total", Range("A1:BA1"), 0)).Formula = [U][B]???[/B][/U]

In this example, WeeklyTotalsAddRow is equal to 5, so the code would write "=SUM(D5:F5)" in G5 and result in "6" (previous rows already have sum formulas in Total column):
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Set[/TD]
[TD]Co.[/TD]
[TD]Sale1[/TD]
[TD]Sale2[/TD]
[TD]Sale3[/TD]
[TD]Total[/TD]
[/TR]
[TR]
[TD]Joe[/TD]
[TD]X4[/TD]
[TD]456[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]=SUM(D1:F1)[/TD]
[/TR]
[TR]
[TD]Jim[/TD]
[TD]X5[/TD]
[TD]789[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]=SUM(D2:F2)[/TD]
[/TR]
[TR]
[TD]Jill[/TD]
[TD]X6[/TD]
[TD]101[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]=SUM(D3:F3)[/TD]
[/TR]
[TR]
[TD]Jess[/TD]
[TD]X7[/TD]
[TD]112[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Here it would write "=SUM(D5:G5)" in H5 and result in "8":
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Set[/TD]
[TD]Co.[/TD]
[TD]Sale1[/TD]
[TD]Sale2[/TD]
[TD]Sale3[/TD]
[TD]Sale4[/TD]
[TD]Total[/TD]
[/TR]
[TR]
[TD]Joe[/TD]
[TD]X4[/TD]
[TD]456[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]=SUM(D1:G1)[/TD]
[/TR]
[TR]
[TD]Jim[/TD]
[TD]X5[/TD]
[TD]789[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]=SUM(D2:G2)[/TD]
[/TR]
[TR]
[TD]Jill[/TD]
[TD]X6[/TD]
[TD]101[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]=SUM(D3:G3)[/TD]
[/TR]
[TR]
[TD]Jess[/TD]
[TD]X7[/TD]
[TD]112[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I can figure out how to select the cell above it and autofill down, but the formula in Total may or may not be a straight SUM like I want, so that's not reliable.

Thanks!

Josh in IN
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Does it have to be with VBA? 'cos i worked out a formula that works


Excel 2010 32 bit
ABCDEFGHIJKL
1NameSetCo.Sale1Sale2Sale3Sale4Sale5Sale6Sale7Sale8Total
2JoeX44561181234525
3JimX57891412567834
4JillX61013533456736
5JessX71123720003217
Sheet8
Cell Formulas
RangeFormula
L2=SUM(D2:INDIRECT(CHAR(65+MATCH("Total",$1:$1,0)-2)&ROW()))
 
Upvote 0
That seems to work fine if the column is AA or greater but it returns 0 when in columns E-Z. Thanks though! I appreciate your effort.
 
Upvote 0
That seems to work fine if the column is AA or greater but it returns 0 when in columns E-Z. Thanks though! I appreciate your effort.

Except you meant the reverse, it would work for columns D to Z, the example I posted above works in column L and you can test in other "SINGLE LETTERED" columns

Do you strictly need VBA or does a regular excel formula work for you?
 
Upvote 0
When I tested it on my actual data I found it worked with double-lettered columns, not single-lettered columns. I can't explain it because I don't fully grasp what you did.

A formula works fine--I think I can just use the macro recorder to "translate" it into VBA.

Thanks again for your help!
 
Upvote 0
When I tested it on my actual data I found it worked with double-lettered columns, not single-lettered columns. I can't explain it because I don't fully grasp what you did.

A formula works fine--I think I can just use the macro recorder to "translate" it into VBA.

Thanks again for your help!

The formula is getting longer by the minute :) :) please test to see that this works for all cases


Excel 2010 32 bit
ABCDEFGHIJ
1NameSetCo.Sale1Sale2Sale3Sale4Sale7Sale8Total
2JoeX445611814823
3JimX578914127520
4JillX610135336727
5JessX711237203217
Sheet8
Cell Formulas
RangeFormula
J2=SUM(D2:INDIRECT(LEFT(SUBSTITUTE(ADDRESS(1,MATCH("Total",$1:$1,0)-1),"$",""), LEN(SUBSTITUTE(ADDRESS(1,MATCH("Total",$1:$1,0)-1),"$",""))-1) & ROW()))
 
Upvote 0
An easier variant to adopt in VBA

You only need to change the "ReqRow" to the row where you want the formula inserted, i have assumed row 2 for this example
Code:
Sub DisplayFormula()
    Dim coladrr As String
    Dim Coladdr2 As String
    Dim ReqRow As Long
    
    'Row to insert the formula
    ReqRow = 2
    
    'Column that has the word "Total"
    coladdr = Split(Range("1:1").Find("Total", Range("A1")).Address, "$")(1)
    
    'Column immediately to the left of the "Total" column
    Coladdr2 = Split(Range("1:1").Find("Total", Range("A1")).Offset(0, -1).Address, "$")(1)
    
    'Inserts the formula into the required row of the required column
    Cells(ReqRow, coladdr).Formula = "=SUM(D" & ReqRow & ":" & Coladdr2 & ReqRow & ")"
End Sub
 
Last edited:
Upvote 0
Works! Thank you so much! I really appreciate it.
 
Upvote 0
Try this: -
No formulae to slow down the job either.
Code:
Option Explicit
'   To start numbering from 1
Option Base 1
Sub VBA_Total()
'   Dimension variables
Dim myTitle As String, RngLen As Integer, _
    myArr() As Long, i As Long, _
    Ans As Long, Num_Cols As Long, Num_Rows As Long
    Dim myTotal, j
    Application.ScreenUpdating=False
' Find_Total
    Cells(1, 1).Select
    Cells.Find(What:="Total", After:=ActiveCell, LookIn:=xlValues, _
          LookAt:=xlPart, SearchOrder:=xlByColumns, _
          SearchDirection:=xlNext).Activate

' Select length of range to use, Discount the 1st 3 columns and the total column

    RngLen = Range(ActiveCell.Address & ":A1").Count - 4

' Count the number of Columns to use:
    ActiveCell.Offset(1, -RngLen).Activate
    Num_Cols = Range(Selection, Selection.End(xlToRight)).Count

' Count the number of rows to use:
    Num_Rows = Range(Selection, Selection.End(xlDown)).Count

' Instead of inserting a  formula for each row, we are going to calculate the answers _
    in RAM and place the values as a range selection which is at least 240x faster. _
    The first row to the last row is loaded into the array. _
    Set up a three dimensional array based upon the active selection, _
    Two dimensions for the existing data and the third dimension's _
    for the totals

    Ans = Num_Rows
    ReDim myArr(1 To Num_Rows, 1 To Ans)

'   a double nested dual-loop will transfer data to the array
    For j = 1 To Num_Rows
        myTotal = 0
        For i = 1 To Num_Cols
            myArr(j, i) = ActiveCell.Offset(0, i - 1).Value
            myTotal = myArr(j, i) + myTotal
        Next i
        myArr(j, 1) = myTotal
        With ActiveCell
            .Offset(, Num_Cols).Value = myArr(j, 1)
            .Offset(1, 0).Select
        End With
    Next j
    Application.ScreenUpdating=True
End Sub
 
Last edited:
Upvote 0
It sounds like you already have the variable WeeklyTotalsAddRow so just add the following code

Code:
Dim rTotal As Range

Set rTotal = Rows(1).Find(What:="Total", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not rTotal Is Nothing Then Cells(WeeklyTotalsAddRow, rTotal.Column).FormulaR1C1 = "=SUM(RC4:RC[-1])"
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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