PDA

View Full Version : Sum function in other sheet



mikkelw
06-11-2020, 02:57 PM
I'm trying to make a sum function, where I will get the data from another sheet (same excel fil)

Which code should I use?

I have tried to use "Application.WorksheetFunction.Sum(r)"
But then I get the answer and not the sum function.

Paul_Hossler
06-11-2020, 04:14 PM
I'm guessing that you don't want the just the answers like in A1 and B2 below, but a formula like in C3?????




Option Explicit


Sub demo()
Dim r1 As Range, r2 As Range

Set r1 = Worksheets("Sheet1").Cells(1, 1).CurrentRegion
Set r2 = Worksheets("Sheet1").Range("F8")

Set r2 = Range(r2, r2.End(xlDown))

Worksheets("Sheet2").Range("A1").Value = Application.WorksheetFunction.Sum(r1)
Worksheets("Sheet2").Range("B2").Value = Application.WorksheetFunction.Sum(r2)

Worksheets("Sheet2").Range("C3").Formula = "=SUM(" & r1.Parent.Name & "!" & r1.Address & ")"
End Sub

Bob Phillips
06-12-2020, 10:48 AM
I think you mean something like


Range("A1").Formula = "=SUM(Sheet1!A1:A10)"

Paul_Hossler
06-12-2020, 01:29 PM
I think you mean something like


Range("A1").Formula = "=SUM(Sheet1!A1:A10)"


If there's data in A1:A10, and ...


Set r1 = Worksheets("Sheet1").Cells(1, 1).CurrentRegion

... doesn't that work out to the same thing?