Using SUM() in VBA

前端 未结 4 597
离开以前
离开以前 2021-01-11 11:46

If I have a set of cells in a worksheet that I want to add up, I can use the formula:

=SUM(Sheet1!A1:A10)

To do this in a sub, I would use:

4条回答
  •  情歌与酒
    2021-01-11 12:37

    You need to use the loop to perform the calculation across all of the sheets, it's the most efficient way by the looks of things. As mentioned above you can type each range separately instead.

    Might be worth converting the range your adding up to a double (or single, integer, etc) because sometimes VBA reads numbers as text.

    v = v + Cdbl(Sheets(i).Range("B2"))

    The reason why you're having issues with Application.WorksheetFunction.Sum("Sheet1:Sheet3!B2") is because if you type that formula into Excel, the range 'Sheet1:Sheet3!B2' won't be recognised by excel.

    To use a Application.WorksheetFunction it has to work in excel outside of VBA.

    Hope that helps.

提交回复
热议问题