Consulting

Results 1 to 4 of 4

Thread: Sum function in other sheet

  1. #1
    VBAX Newbie
    Joined
    Jun 2020
    Posts
    3
    Location

    Angry Sum function in other sheet

    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.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by Bob Phillips View Post
    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?
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •