Excel VBA Sum from Multiple Sheets

前端 未结 3 433
情话喂你
情话喂你 2021-01-26 04:01

I am trying to create a function or functions that can sum daily hours from time cards for each client to come up with the total hours worked per day. Each client has it\'s own

3条回答
  •  旧时难觅i
    2021-01-26 04:57

    So, an example formula would look like this:

    =SUM(Sheet2!A1:A5,Sheet3!A1:A5,Sheet4!A1:A5)

    That would sum Sheet2-Sheet4, A1:A5 on all sheets.

    Is there a reason you need to write the VBA code to do this?

    Can't you just enter it as a formula once?

    Also, if you're going to the trouble of writing VBA to generate a formula, it may make more sense to just do the sum entirely in VBA code.

    If not, try this:

    Sub GenerateTheFormula()
    Dim x, Formula
    Formula = "=SUM(" 'Formula begins with =SUM(
    For x = 3 To Sheets.Count
        Formula = Formula & Sheets(x).Name & "!A1," 'Add SheetName and Cell and Comma
    Next x
    Formula = Left(Formula, Len(Formula) - 1) & ")" 'Remove trailing comma and add parenthesis
    Range("B1").Formula = Formula 'Where do you want to put this formula?
    End Sub
    

    Results:

提交回复
热议问题