我不确定标题是否准确地描述了我的查询,因此,我将尽力在此处进行描述。
我有一个跟踪支出和收入的工作表,还有一个宏,可用于将复选框插入选定的单元格,将复选框链接到这些单元格,最后,一旦复选框为检查,如果再次取消检查,则同样。
这是执行此操作的代码:
子:
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:无复选框
图片2:已添加复选框
图片3:选中了一个复选框
图片4:已选中2个复选框
答案 0 :(得分:1)
您可以使用条件格式和SUMIF
公式来实现此目标
我使用了以下条件格式设置规则(您将需要针对您的范围进行更改)
条件格式既应用于单元格填充,也应用于字体文本颜色(以使True
/ False
为“不可见”)
在单元格C6
(合并范围)中,我有公式
=SUMIF($D$3:$D$5,TRUE,$C$3:$C$5)
D
范围中的单元格包含复选框所链接的单元格的值(即True
,False
),而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)