+ Reply to Thread
Results 1 to 7 of 7

Sum Across sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2013
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    31

    Sum Across sheets

    I have a series of 12 sheets each named as a month
    I wish to find the cumulative value of a cell in each month

    So the January sheet would have a value .Range("AP9").Value
    the February Sheet would have a value January!AP9 + February!AP9
    March January!Ap9 + February!AP9 + March!AP9 and so on

    I triedto achieve this with the following code
    Sub updatesummary()
    Dim ws as workSheet
    Sformula = ""
    For Each ws In ActiveWorkbook.Worksheets
    
        With Sheets(ws.Name)
       
            If ws.Name <> "Template" Then
                     If ws.Name <> "Names" Then
                        If Left(ws.Name, 3) <> "SUM" Then
        
                                
                                     Sformula = Sformula & ws.Name & "!" & .Range("AP9").Address & " +"
                                
                                       Sformula = "= " & Left(Sformula, Len(Sformula) - 2)
                                       .Range("AQ9").FormulaR1C1 = Sformula
                                       Sformula = Sformula & "+"
                                       Sformula = Right(Sformula, Len(Sformula) - 3)
                                .Range("AQ9").Copy
                                    .Range("AQ10:AQ57").PasteSpecial Paste:=xlPasteFormulas
                                    
                                    Range("AQ9:AQ57").Copy
                                    Range("AQ9:AQ57").PasteSpecial Paste:=xlPasteValues
                                    Application.CutCopyMode = False
    
                                
                         End If
                 End If
            End If
           
    
     End With
    Next
    
       
    End Sub
    But this does nor work, the initial = sign causes an error, but without the sign all I have is a string not a formula
    I need some help please
    LoftySuth

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Sum Across sheets

    Check this out...........in each sheet, cell AP9 contains the monthly amount and cell B10 contains the accumulative YTD amount:

    Sub YearToDate()
        Dim w(1 To 12) As Worksheet
        For i = 1 To 12
            Set w(i) = Sheets(i)
        Next i
        w(1).Range("B10").Formula = "=AP9"
        For i = 2 To 12
            w(i).Range("B10").Formula = "=" & w(i - 1).Name & "!B10+AP9"
        Next i
    End Sub
    Note that the January formula is unlike the other 11 formulas.
    Gary's Student

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,959

    Re: Sum Across sheets

    Unless you reallywant to use a macro for this, a relatively simple =SUM() will give you the results you need...
    =SUM('Jan:Dec'!A1)

    Adjust sheet names as needed
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Sum Across sheets

    I was also working on this.

    combining my effort with Jakobshavn's:-

    
    Sub YearToDate()
        Sheets(1).Range("B10").Formula = "=AP9"
        For i = 2 To 12
            Sheets(i).Range("B10").Formula = "=" & Sheets(i - 1).Name & "!B10+AP9"
        Next i
    End Sub

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Sum Across sheets

    Alternatively you could use the Month Names instead of assuming that the sheets are sequential.

    This is probably the most robust solution.

    
    Sub YearToDate2()
        Sheets(MonthName(1)).Range("B10").Formula = "=AP9"
        For i = 2 To 12
            Sheets(MonthName(i)).Range("B10").Formula = "=" & Sheets(MonthName(i - 1)).Name & "!B10+AP9"
        Next i
    End Sub

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Sum Across sheets

    it occurs to me that you might want to sort the sheets first.

    
    For ws = 1 To 11
    Sheets(MonthName(ws)).Move before:=Sheets(ws + 1)
    Next
    End Sub

  7. #7
    Registered User
    Join Date
    10-14-2013
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Sum Across sheets

    Thank you all for your ideas. The key revelation to me was Sheets.Range.Formula. I don't really want to use a cumulative Cell (B10) but I think I will be able to write an accumulating string as in my original post (SFormula = SFormula &.....). Once again many thanks for all your contributions my jaw dropped when I opened up the Forum!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 10-04-2013, 04:21 AM
  2. [SOLVED] Macro to Unprotect sheets-unlock range of cells-protect sheets
    By jrace in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2013, 10:45 AM
  3. Replies: 17
    Last Post: 02-01-2013, 12:20 PM
  4. Replies: 2
    Last Post: 01-18-2012, 05:08 AM
  5. Macro for filter on protected workbook that works for all sheets, no matter what sheets are named?
    By StargateFanFromWork in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-26-2006, 02:35 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1