Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an excel spreadsheet with multiple columns. All I want to do is sum each column and put the result in the row after the last row. It sounds simple enough; however, the formulas do not show.

What I have tried:

Here is my code . Any help would be appreciated. I'm pulling my hair out here. 


       For c = 2 To 6 Step 1
          ColumnLetter = Split(Cells(1, c).Address, "$")(1)
          strFormula = "=SUM(" & ColumnLetter & "2:" & ColumnLetter & (lastrow - 1) & ")"
          Cells(c, lastrow).Formula = strFormula
       Next

I have also tried Cells(c,lastrow).Value = strFormula with no success
Posted
Updated 24-Sep-20 6:37am
v2
Comments
Richard MacCutchan 24-Sep-20 12:24pm    
What do you actually get in each column?
Member 13029237 24-Sep-20 12:34pm    
Thanks for responding, I figured it out. It was a stupid error.
Richard MacCutchan 24-Sep-20 12:31pm    
Have you defined lastrow correctly?
Member 13029237 24-Sep-20 12:34pm    
Thanks for responding, I figured it out. It was a stupid error.

Try this:
VB
lastrow = ' set this to the row that will contain the formulas
For c = 2 To 6 Step 1
   ColumnLetter = Split(Cells(1, c).Address, "$")(1)
   strFormula = "=SUM(" & ColumnLetter & "2:" & ColumnLetter & (lastrow - 1) & ")"
   Cells(lastrow, c).Formula = strFormula
Next
 
Share this answer
 
Figured it out. I had the Column before the row in the Cells command.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900