VBA - 如果选中复选框,则将单元格值添加到总和

问题描述 投票:0回答:2

我不确定标题是否准确描述了我的查询,所以我会尽力在这里描述它。

我有一张跟踪费用和收入的工作表,我有一个用于将复选框插入选定单元格的宏,将复选框链接到这些单元格,最后,一旦选中复选框,应用条件格式的条件,同样如果再次取消选中。

这是执行此操作的代码:

子:

Sub Insert_Checkbox_Link_Cell()

    Dim rngCel, myCells As Range
    Dim ChkBx As CheckBox
    Dim cBx As Long

    Set myCells = Selection

    myCells.NumberFormat = ";;;"

    Application.ScreenUpdating = False

    For Each rngCel In myCells

        With rngCel.MergeArea.Cells

            If .Resize(1, 1).Address = rngCel.Address Then

                Set ChkBx = ActiveSheet.CheckBoxes.Add(.Left, .Top, .Width, .Height)

                With ChkBx

                    .Value = xlOff
                    .LinkedCell = rngCel.MergeArea.Cells.Address
                    .Text = ""
                    .Width = 18
                    .Top = rngCel.Top + rngCel.Height / 2 - ChkBx.Height / 2
                    .Left = rngCel.Left + rngCel.Width / 2 - ChkBx.Width / 2
                    .Select

                    'Function Call
                    Selection.OnAction = "Change_Cell_Colour"

                End With

            End If

        End With

    Next rngCel

    If (Range(ChkBx.LinkedCell) = "True") Then

        myCells.Interior.ColorIndex = 43

    Else

        myCells.Interior.ColorIndex = 48

    End If

    Application.ScreenUpdating = True

End Sub

功能:

Function Change_Cell_Colour()

    Dim xChk As CheckBox
    Dim clickedCheckbox As String

    clickedCheckbox = Application.Caller

    Set xChk = ActiveSheet.CheckBoxes(clickedCheckbox)

    If xChk.Value = 1 Then

        ActiveSheet.Range(xChk.LinkedCell).Interior.ColorIndex = 43

    Else

        ActiveSheet.Range(xChk.LinkedCell).Interior.ColorIndex = 48

    End If

End Function

那么这是如何工作的,我选择了我想要复选框的单元格范围,然后我运行宏并插入如上所述的复选框。

现在我想再增加一点,我不确定是否有可能。

在下图中,我列出了收入,底部是总收入。因此,当钱进来时,复选框被选中。

我想做的是:

虽然复选框是UNCHECKED,但我不希望单元格中的值添加到底部的总计数中。

当它是CHECKED时,则应将单元格中的值添加到底部的总计数中。

图1:没有复选框

enter image description here

图2:添加了复选框

enter image description here

图3:检查了一个复选框

enter image description here

图4:2复选框已选中

enter image description here

excel vba checkbox
2个回答
1
投票

您可以使用条件格式和SUMIF公式来实现此目的

enter image description here

我使用了以下条件格式规则(您需要为您的范围更改此值)

enter image description here

条件格式既适用于单元格填充,也适用于字体文本颜色(使True / False成为'隐形')

在单元格C6(合并范围)我有公式

=SUMIF($D$3:$D$5,TRUE,$C$3:$C$5)

D范围内的单元格包含复选框的链接单元格的值(即TrueFalse),C范围是您想要求和的值。

这是一个比任何VBA解决方案简单得多的方法,而且就个人而言,我将从上面的vba中删除单元格的格式,并使用条件格式。

如果你正在寻找一个VBA方式来启动这个(除了SUMIF公式)我已经更新了你的下面的代码来添加条件格式

Sub Insert_Checkbox_Link_Cell()
    Dim rngCel, myCells As Range
    Dim ChkBx As CheckBox
    Dim cBx As Long

    Set myCells = Selection
    myCells.NumberFormat = ";;;"

    Application.ScreenUpdating = False
    For Each rngCel In myCells
        With rngCel.MergeArea.Cells
            If .Resize(1, 1).Address = rngCel.Address Then
                Set ChkBx = ActiveSheet.CheckBoxes.Add(.Left, .Top, .Width, .Height)
                With ChkBx
                    .Value = xlOff
                    .LinkedCell = rngCel.MergeArea.Cells.Address
                    .Text = ""
                    .Width = 18
                    .Top = rngCel.Top + rngCel.Height / 2 - ChkBx.Height / 2
                    .Left = rngCel.Left + rngCel.Width / 2 - ChkBx.Width / 2
                End With
            End If
        End With
    Next rngCel

    With myCells
        ' Set default value
        .Value2 = False
        ' Add conditional formatting for False value
        With .FormatConditions
            .Add Type:=xlExpression, Formula1:="=" & myCells.Cells(1).Address(False, True) & "=False"
        End With
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 9868950
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            With .Font
                .Color = -6908266
                .TintAndShade = 0
            End With
        End With
        ' Add conditional formatting for True value
        With .FormatConditions
            .Add Type:=xlExpression, Formula1:="=" & myCells.Cells(1).Address(False, True) & "=True"
        End With
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 52377
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            With .Font
                .Color = -16724839
                .TintAndShade = 0
            End With
        End With
    End With

    Application.ScreenUpdating = True
End Sub

1
投票

您可以为颜色更改功能中添加了复选框的单元格提供一个值(例如:1表示已选中,0表示未选中)。保持单元格的字体颜色与单元格的填充颜色相同,以便肉眼看不到该值。然后在总和部分,您可以使用sumif函数。

enter image description here

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.