Excel – Is it possible to automatically add cells from a new sheet to a formula in Excel

financemicrosoft excelmicrosoft-excel-2013vbaworksheet-function

I would like to make a new financial spreadsheet for tracking expenses. Since the expense sheets are created as and when I cannot pre-generate a spreadsheet to populate with formulae. I have a summary sheet which adds totals from the other sheets and currently create a formula by hand at the end of the month. Is there a way to automatically sum across multiple sheets and add new sheets to the sum when they are created?

Is this possible using standard Excel or am I going to need to use some VBA?

I know how to add the same cell across multiple sheets already (see: Excel – Formulas that total across multiple sheets?)

This vba script is part of the way to what I am looking to do but not exactly what I want to do: Excel – import data from cell automatically when a new tab is created

P.S. I don't know VBA but willing to learn. I'm used to C/Python.

Best Answer

Say, for instance, you have your running total in Sheet1!A1. Right, so now you create a macro you can run that will take all the totals from the other sheets and sum them.

Let's assume the totals for each sheet are on cell A10 -

Sub updatethesum()
Dim ws As Worksheet
    Dim i As Double
    i = 0
For Each ws In ThisWorkbook.Worksheets

    If ws.Name <> "Sheet1" Then
        i = i + ws.Range("A10")
    End If

Next
Sheets("Sheet1").Range("A1") = i
End Sub

If you're worried someone will put a letter instead of a number in A10 you can restrict the input with something like this -

Sub updatethesum()

Dim ws As Worksheet
    Dim i As Double
    i = 0
    Dim bletter As Boolean

For Each ws In ThisWorkbook.Worksheets

    If ws.Name <> "Sheet1" Then
        bletter = IsNumeric(ws.Range("A10").Value)
            If bletter = True Then
             i = i + ws.Range("A10")
            End If
    End If

Next
Sheets("Sheet1").Range("A1") = i
End Sub
Related Question