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.
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.
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
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3
I think you mean something like
Range("A1").Formula = "=SUM(Sheet1!A1:A10)"
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Last edited by Paul_Hossler; 06-12-2020 at 06:10 PM.
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3