Excel VBA中的合并和求和

时间:2018-11-25 23:53:59

标签: excel vba merge totals

我在Excel中有一个table,我需要它来对单元格B的总数求和,并在最后一个值之后返回该单元格上的值。下面的代码可以做到这一点。

.gitignore

我需要它来将单元格从B合并到H,将文本设置为粗体并更改颜色,然后添加文本“总计” like this

1 个答案:

答案 0 :(得分:0)

Sub Total()

    Dim l As Long

    l = Cells(Rows.Count, "A").End(xlUp).Row

    Range("B" & l + 1) = "=sum(B2:B" & l & ")"

    With Range("A" & l + 1)
        .FormulaR1C1 = "Total"
        .Font.Bold = True
    End With

    With Range("B" & l + 1 & ":H" & l + 1)
        .MergeCells = True
        .Font.Bold = True
        .HorizontalAlignment = xlCenter
    End With

    With Range("A" & l + 1 & ":H" & l + 1).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0

    End With

End Sub