is_utf8:0,
 
Search : in
By :

Calculate the sum of rows using macro

Last answer on Jun 3, 2009 10:57:13 am BST Manhamanha, on Jun 2, 2009 11:56:36 am BST 
 Report this message to moderators

Hello,
I need some help with the folowing problem. I have a table with changing length of rows and columns. I need to calculate the sums of the rows and columns and write it in the next blank cell. I managed to write a working macro for the columns but I have a problem with the rows. Could you please help? Here are the two codes...

Sub AutoSumCol()
Dim lngColumn As Long
Dim lngLastRow As Long
For lngColumn = 1 To Range("A1").End(xlToRight).Column
lngLastRow = Cells(1, lngColumn).End(xlDown).Row
Cells(lngLastRow + 1, lngColumn).Formula = "=Sum(" & Cells(1, lngColumn).Address & ":" & _
Cells(lngLastRow, lngColumn).Address & ")"
Next lngColumn
End Sub

Sub AutoSumRow()
Dim lngRow As Long
Dim lngLastColumn As Long
For lngRow = 1 To Range("A1").End(xlToRight).Row
lngLastColumn = Cells(1, lngRow).End(xlDown).Column
Cells(lngLastColumn + 1, lngRow).Formula = "=Sum(" & Cells(1, lngRow).Address & ":" & _
Cells(lngLastColumn, lngRow).Address & ")"
Next lngRow
End Sub

Thanks :)

Configuration: Windows Vista Internet Explorer 8.0

Best answers for « Calculate the sum of rows using macro » in :
Excel – Macro to detect and hide blank rows Show Excel – Macro to detect and hide blank rows Issue Solution Note Issue I want a macro to unhide about 20 blank rows copy values into the top row then hide the remaining rows (some cells have fill though) then the next time it will...
Excel – Formula for cell calculation ShowExcel – Formula for cell calculation Issue Solution Note Issue Simple formula of =J3-SUM(L3:X3) BUT if J3 is empty then I want the formula to run as =C3-SUM(L3:X3) What is the proper formula to get the calculation to utilize J3 if...
Calculate the power of your Power supply ShowCalculate the power of your Power supply Choosing the right power supply Links Choosing the right power supply When building up your PC it is quite difficult to determine the power supply to be used( this will depend on the...
[Excel] Sorting rows only Show[Excel] Sorting rows only Microsoft Excel by default perform a sort on the columns (Data/Sort). To sort the data horizontally, just select the data to be sorted, then Data tab /Sort/ Options/ check the Sort Left to right box in the...
Download The Love Calculator ShowLove is a big word, then all means are good to find it. The name of each can say about it on compatibility between two persons a lot. The Love Calculator is a program which counting affinity between two individuals. It is not a precise science,...
Spreadsheet - Formulas ShowIntroduction to Formulas The main use of a spreadsheet is to automate calculations, which means using cells to perform operations based on values in other cells. The spreadsheet recalculates all the values each time a change is made to the value of...

1

Excelguru, on Jun 3, 2009 8:58:57 am BST

Hi

The problem seems to be in the line Cells(lngLastColumn + 1, lngRow).Formula = "=Sum(" & Cells(1, lngRow).Address & ":" & _
Cells(lngLastColumn, lngRow).Address & ")"
I think , here you have to use Cells(lngRow,lngLastColumn + 1).

My suggestion is to use activesheet.usedrange.rows.count instead of Range.End(xlToRight) and the like statements.

Please revert back if you have any qurties Winners are losers who got up and gave it one more try. -Dennis DeYoung
My Interests are financial Modelling and custom excel development.

Reply to Excelguru

2

 Manhamanha, on Jun 3, 2009 10:57:13 am BST
  • +1

I managed to deal with both problems with one method. Thanks for the help :)

Reply to Manhamanha