Alex Piotto
Board Regular
- Joined
- Jul 5, 2016
- Messages
- 82
- Office Version
- 2007
- Platform
- Windows
Hi people!
So... see the situation below. I would like to have the sum of each row in a dynamic range, until the last column, also part of a dynamic range.
I may have 2 rows and 2 columns, or 50 rows and 50 columns... number of rows and columns is variable.
And here is the code so far... it surely add only 3 rows... but dynamically at least!
I will use the solution to learn something about loops... it is not a job, actually.
So... see the situation below. I would like to have the sum of each row in a dynamic range, until the last column, also part of a dynamic range.
I may have 2 rows and 2 columns, or 50 rows and 50 columns... number of rows and columns is variable.
And here is the code so far... it surely add only 3 rows... but dynamically at least!
VBA Code:
Private Sub CommandButton4_Click()
Range("I6", Cells(6, Columns.Count).End(xlToLeft)).Select
Range("I6", Cells(6, Columns.Count).End(xlToLeft)).Name = "MyRange"
Dim tcell As Range
Set tcell = Range("G6")
tcell.Formula = "=SUM(Myrange)"
Range("I7", Cells(7, Columns.Count).End(xlToLeft)).Select
Range("I7", Cells(7, Columns.Count).End(xlToLeft)).Name = "MyRange2"
Dim tcell2 As Range
Set tcell2 = Range("G7")
tcell2.Formula = "=SUM(Myrange2)"
Range("I8", Cells(8, Columns.Count).End(xlToLeft)).Select
Range("I8", Cells(8, Columns.Count).End(xlToLeft)).Name = "MyRange3"
Dim tcell3 As Range
Set tcell3 = Range("G8")
tcell3.Formula = "=SUM(Myrange3)"
End Sub
I will use the solution to learn something about loops... it is not a job, actually.