Meesha
Meesha

Reputation: 821

Sum of a column

I want to calculate Sum of different columns in a worksheet and fill it in another worksheet.

LastrowA = Weight.Cells(Weight.Rows.Count, "A").End(xlUp).Row
Set Rng = Weight.Range("A2" & LastrowA)
Weight.Activate

Summ= WorksheetFunction.Sum(Rng)    ' Doesn't work
Summary.Cells(1, 1).Value=Summ

Summary.Cells(1, 1).Value = Application.Sum(Rng)  ' Doesn't Work

The two Sheets are Weight and Summary. I have tried above two ways and both give me an answer of Zero. I want to continue doing it for all my columns . Please advice. Thank you.

Upvotes: 1

Views: 61

Answers (2)

Anurag Singh
Anurag Singh

Reputation: 120

This sub will sum data in Sheet 1 Columns A to C and put results in Sheet2

You can use this sub and just change column letters and output cells.

Hope this helps

Sub SumRange()
Dim wb as Workbook
Set wb = Thisworkbook
Dim ws as worksheet
Set Weight = wb.Sheets("Weight")

LastRow1 = Weight.Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Weight.Range("A2:A" & "" & LastRow1 & "")
Col1Sum = WorksheetFunction.Sum(Rng)

LastRow1 = Weight.Range("B" & Rows.Count).End(xlUp).Row
Set Rng = Weight.Range("B2:B" & "" & LastRow1 & "")
Col2Sum = WorksheetFunction.Sum(Rng)

LastRow1 = Weight.Range("C" & Rows.Count).End(xlUp).Row
Set Rng = Weight.Range("C2:C" & "" & LastRow1 & "")
Col3Sum = WorksheetFunction.Sum(Rng)

ThisWorkbook.Sheets("Sheet2").Cells(2, 2).Value = Col1Sum
ThisWorkbook.Sheets("Sheet2").Cells(3, 2).Value = Col2Sum
ThisWorkbook.Sheets("Sheet2").Cells(4, 2).Value = Col3Sum

End Sub

Upvotes: 1

winegum
winegum

Reputation: 43

first of all, i have no excel here, so i cant try by myself what I'm thinking. but i think you use the Range-Method the wrong way.. it should look like this:

Set Rng = Weight.Range("A2" , Cells( LastrowA , "A") )

so there is a "," between the arguments instead of a "&" an there is a second Cell instead of a row-number. Hope that helps

Upvotes: 1

Related Questions