Consulting

Results 1 to 15 of 15

Thread: Solved: Sum across worksheets VBA

  1. #1
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location

    Solved: Sum across worksheets VBA

    Hi Everyone,


    I have this formula below that will sum across worksheets, in my example workbook below.

    =SUM('06-01-2007:06-06-2007'!C:C)


    But I would like to convert it into a vba code to get the First & Last date of the worksheets in my workbook BUT ignore “Performance Data”, “Individual Report” & “Comparison Report”.

    The dates of the worksheets will vary time to time in my workbook. So if the code could get the First and Last date and apply into the formula that will be great.
    SHAZAM!

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Since your sheet names can be recognised by Excel as dates I used the IsDate function:

    Sub blah()
    For Each sht In ActiveWorkbook.Sheets
    If IsDate(sht.Name) Then mytotal = mytotal + Application.WorksheetFunction.Sum(sht.Columns("C"))
    Next sht
    MsgBox "Total = " & mytotal
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by p45cal
    Since your sheet names can be recognised by Excel as dates I used the IsDate function:

    Sub blah()
    For Each sht In ActiveWorkbook.Sheets
    If IsDate(sht.Name) Then mytotal = mytotal + Application.WorksheetFunction.Sum(sht.Columns("C"))
    Next sht
    MsgBox "Total = " & mytotal
    End Sub

    Thank you very much for the code. Actually I sent the wrong sample book. The right sample has the correct worksheets names. The name of the worksheets contains Texts. So when I ran you your code I get a blank result. Anyway to get around that?

    Also can we input the results in cell B2?
    SHAZAM!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub blah()
    Dim sht As Worksheet
    Dim myTotal As Double

    For Each sht In ActiveWorkbook.Sheets
    If IsDate(Replace(ActiveSheet.Name, "Test ", "")) Then
    myTotal = myTotal + Application.Sum(sht.Columns("C"))
    End If
    Next sht
    Range("B2").Value = myTotal
    End Sub


    [/vba]
    ____________________________________________
    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

  5. #5
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by xld
    [vba]

    Sub blah()
    Dim sht As Worksheet
    Dim myTotal As Double

    For Each sht In ActiveWorkbook.Sheets
    If IsDate(Replace(ActiveSheet.Name, "Test ", "")) Then
    myTotal = myTotal + Application.Sum(sht.Columns("C"))
    End If
    Next sht
    Range("B2").Value = myTotal
    End Sub
    [/vba]

    Hi xld Thank you for replying. I ran your code on worksheet "Performance Data" but I get a zero result. I'm sorry I should have mention that I would like to run the code on "Performance Data" and have the result in cell B2 in a formula.

    Something like this,

    [vba]
    Range("B2").Formula = myTotal
    [/vba]
    SHAZAM!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry Shaz, typo

    [vba]

    Sub blah()
    Dim sht As Worksheet
    Dim myTotal As Double

    For Each sht In ActiveWorkbook.Sheets
    If IsDate(Replace(sht.Name, "Test ", "")) Then
    myTotal = myTotal + Application.Sum(sht.Columns("C"))
    End If
    Next sht
    Range("B2").Value = myTotal
    End Sub
    [/vba]
    ____________________________________________
    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

  7. #7
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Thank You so much xld.

    One more thing instead of the results comming as a vaule can it be in a formula result instead?

    I tried to modify this line,

    Range("B2").Value = myTotal

    into this

    Range("B2").Formula = myTotal


    But no avail.
    SHAZAM!

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If it could be a formula, then you would add a formula in Excel would you not?

    is it just because you don't knoW the names of the first and last workbooks when you run it?
    ____________________________________________
    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

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    something like this at the end?:

        mystr = "=""the total is "" & " & myTotal
        Range("B2").Formula = mystr
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

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

  11. #11
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by xld
    If it could be a formula, then you would add a formula in Excel would you not?
    Yes I could But this is a daily routine. The code you provided will be a Sub Workbook_Open event. So when ever a user open the workbook the macro will trigger.

    Quote Originally Posted by xld
    is it just because you don't knoW the names of the first and last workbooks when you run it?
    Yes I don't know the first and last names of the worksheets.


    Also the reason why I would like a formula result shown is because when the supervisor open the workbook the formula will tell him or her how the values were calculated.

    I left a expected result in cell D2 in the workbook below.

    Is possible to have it in a formula?
    SHAZAM!

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Of course, but it does rely on the worksheets being in order, the non-SUM ones that is

    [vba]

    Private Sub Workbook_Open()

    Dim sht As Worksheet
    Dim myTotal As Double
    Dim sFirst As String
    Dim sLast As String
    Dim i As Long

    For i = 1 To ThisWorkbook.Worksheets.Count
    Set sht = ThisWorkbook.Worksheets(i)
    If IsDate(Replace(sht.Name, "Test ", "")) Then
    If sFirst = "" Then sFirst = sht.Name
    sLast = sht.Name
    End If
    Next i
    Range("B2").Formula = "=SUM('" & sFirst & ":" & sLast & "'!C:C)"
    End Sub
    [/vba]
    ____________________________________________
    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

  13. #13
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by xld
    Of course, but it does rely on the worksheets being in order, the non-SUM ones that is
    Yes the worksheets will always be in order.

    Quote Originally Posted by xld
    [vba]

    Private Sub Workbook_Open()

    Dim sht As Worksheet
    Dim myTotal As Double
    Dim sFirst As String
    Dim sLast As String
    Dim i As Long

    For i = 1 To ThisWorkbook.Worksheets.Count
    Set sht = ThisWorkbook.Worksheets(i)
    If IsDate(Replace(sht.Name, "Test ", "")) Then
    If sFirst = "" Then sFirst = sht.Name
    sLast = sht.Name
    End If
    Next i
    Range("B2").Formula = "=SUM('" & sFirst & ":" & sLast & "'!C:C)"
    End Sub
    [/vba]
    Perfect thank you very much. Now with your solution I could apply with other formulas that I use with 3D reference. This will save me allot of caluclation time. Once again thank You very much!!
    SHAZAM!

  14. #14
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    220
    Location
    I not sure if this is what you need in the way of a formula.
    =SUM('*'!C:C) this will sum all the sheets in the range C:C except the sheet the formula resides on. So used as a summary sheet calculation you could use VBA to put the formula in the cell you will use for summing.

    [vba]Range("D2").Formula = "=SUM('*'!C:C)"[/vba]
    "Intellectual passion occurs at the intersection of fact and implication."

    SGB

  15. #15
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by Carl A
    I not sure if this is what you need in the way of a formula.
    =SUM('*'!C:C) this will sum all the sheets in the range C:C except the sheet the formula resides on. So used as a summary sheet calculation you could use VBA to put the formula in the cell you will use for summing.

    [vba]Range("D2").Formula = "=SUM('*'!C:C)"[/vba]
    Thanks Carl A,

    Neat trick!!
    SHAZAM!

Posting Permissions

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