Sum dynamic columns

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello all

I hope you can help on this

I have data values that starts in [Sheets("DashBoard").Range("C2")] with multiple rows and multiple columns.

Both the Rows and Columns can expand or contract in size every time the report is run

Using VBA how can I find the last column with data then sum the total of each row after the last column of data

Can anyone please help on this
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hello,

Do not know the ' dynamic sum ' you might need ...

But you could have a Named Range dynamically defined ..
 
Upvote 0
This is close to what I need

[Sub SumAllColumns()
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For col = 3 To lastCol
lastRow = Cells(Rows.Count, col).End(xlUp).Row
colSum = WorksheetFunction.Sum(Range(Cells(2, col), Cells(lastRow, col)))
Cells(lastRow + 1, col) = colSum
Next col
End Sub]

How can I change this code please to find the last column and then add the total for each row.

I have been searching the web trying to find an answer but so far I can't find what I need
 
Upvote 0
Hi
Try
VBA Code:
Sub test()
    Dim i As Double
    Dim a, b
    a = Cells(1).Offset(1).Resize(Cells(Rows.Count, 1).End(xlUp).Row - 1, Cells(2, Columns.Count).End(xlToLeft).Column)
    ReDim b(1 To UBound(a))
    For i = 1 To UBound(a)
        b(i) = SumNRowsAtXColumnInArray(Application.Transpose(a), i)
    Next
    Cells(1, 1).Offset(1, UBound(a, 2)).Resize(UBound(b)) = Application.Transpose(b)
End Sub
Function SumNRowsAtXColumnInArray(varArray As Variant, x As Double) As Variant
    Dim lngLoop As Long
    Dim varSum
    For lngLoop = 1 To UBound(varArray, 1)
        varSum = varSum + varArray(lngLoop, x)
    Next lngLoop
    SumNRowsAtXColumnInArray = varSum
    lngLoop = Empty
    varSum = Empty
End Function
 
Upvote 0
Try:
VBA Code:
Sub SumRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lCol As Long, x As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    For x = 1 To LastRow
        lCol = Cells(x, Columns.Count).End(xlToLeft).Column
        Cells(x, lCol + 1) = WorksheetFunction.Sum(Range("A" & x).Resize(, lCol))
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi
Try
VBA Code:
Sub test()
    Dim i As Double
    Dim a, b
    a = Cells(1).Offset(1).Resize(Cells(Rows.Count, 1).End(xlUp).Row - 1, Cells(2, Columns.Count).End(xlToLeft).Column)
    ReDim b(1 To UBound(a))
    For i = 1 To UBound(a)
        b(i) = SumNRowsAtXColumnInArray(Application.Transpose(a), i)
    Next
    Cells(1, 1).Offset(1, UBound(a, 2)).Resize(UBound(b)) = Application.Transpose(b)
End Sub
Function SumNRowsAtXColumnInArray(varArray As Variant, x As Double) As Variant
    Dim lngLoop As Long
    Dim varSum
    For lngLoop = 1 To UBound(varArray, 1)
        varSum = varSum + varArray(lngLoop, x)
    Next lngLoop
    SumNRowsAtXColumnInArray = varSum
    lngLoop = Empty
    varSum = Empty
End Function
Mohadin thank you for your reply
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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