Using SUM() in VBA

前端 未结 4 559
离开以前
离开以前 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:27

     Sub SumWorksheets()
        Dim ws As Worksheet
        Dim v As Variant
        For Each ws In ThisWorkbook.Worksheets
    '    If ws.Name <> ThisWorkbook.ActiveSheet.Name Then ' (Sum other sheets only)
        If ws.Name <> "" Then
        Application.DisplayAlerts = False
        v = v + ws.Range("B2")
        Application.DisplayAlerts = True
        End If
        Next ws
        MsgBox v
        End Sub
    

提交回复
热议问题