我不确定标题是否准确描述了我的查询,所以我会尽力在这里描述它。
我有一张跟踪费用和收入的工作表,我有一个用于将复选框插入选定单元格的宏,将复选框链接到这些单元格,最后,一旦选中复选框,应用条件格式的条件,同样如果再次取消选中。
这是执行此操作的代码:
子:
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复选框已选中
您可以使用条件格式和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