[英]VBA - Highlight Cell With Checkbox
我的流程的一些逻辑:
在工作表的K列中,我使用开发人员标签插入了单元格K3-K53中的复选框(将来可能会更长)。
然后,我将复选框与放置该单元格的位置相关联。
我通过转到“设置单元格格式”,单击“自定义”,然后键入“ ;;;”来格式化此列中的单元格。 这是为了隐藏 “ True / False”文本。
我的下一步是根据文本更改单元格颜色。
注意:
我已经搜索了几个论坛,并结合了所有论坛的一些代码示例,因此我将无法准确地引用源,但是下面是到目前为止的内容:
码:
Sub Change_Cell_Colour()
Dim xName As Integer
Dim xChk As CheckBox
Dim rng As Range
Dim lRow As Long
lRow = ActiveWorksheet.Cells(Rows.Count, "B").End(xlUp).Row
Set rng = ActiveWorksheet.Range("K2:K" & lRow)
For Each xChk In ActiveSheet.CheckBoxes
xName = Right(xChk.Name, Len(xChk.Name) - 10)
If (Range(xChk.LinkedCell) = "True") Then
rng.Interior.ColorIndex = 6
Else
rng.Interior.ColorIndex = xlNone
End If
Next
End Sub
我在尝试获取最后一行的行上不断出现错误。
码:
lRow = ActiveWorksheet.Cells(Rows.Count, "B").End(xlUp).Row
错误:
所需对象
我什至不确定我所拥有的代码是否可以解决我的问题,因此,对于解决主要问题的任何帮助( 基于是否选中复选框,突出显示一个单元格)都将不胜感激。
这是快速重写的带有注释的批注,说明:
Sub Change_Cell_Colour()
Dim xChk As CheckBox
'Be explicit about which worksheet. Leaving it to "Activeworksheet" is going to cause problems
' as we aren't always sure which sheet is active...
'Also in this case we don't need to know the last row. We will iterate checkbox objects, not
' populate rows.
'lRow = ActiveWorksheet.Cells(Rows.Count, "B").End(xlUp).Row
'Again... we don't need this. We just need to iterate all the checkboxes on the sheet
'Set rng = ActiveWorksheet.Range("K2:K" & lRow)
'This is good stuff right here, just change the ActiveSheet to something more explicit
' I've changed this to the tab named "Sheet1" for instance.
For Each xChk In Sheets("Sheet1").CheckBoxes
'Getting the name of the checkbox (but only the last 10 characters)
xName = Right(xChk.Name, Len(xChk.Name) - 10)
'We can check the linked cell's value, but we can also just check if the
' if the checkbox is checked... wouldn't that be easier?
'If (Range(xChk.LinkedCell) = "True") Then
If xChk.Value = 1 Then
'Now we can use the "LinkedCell", but it's a STRING not a RANGE, so we will have
' to treat it as the string name of a range to use it properly
Sheets("Sheet1").Range(xChk.LinkedCell).Interior.ColorIndex = 6
Else
Sheets("Sheet1").Range(xChk.LinkedCell).Interior.ColorIndex = xlNone
End If
Next
End Sub
这是准系统版本,只是为了使其正常工作
Sub Change_Cell_Colour()
Dim xChk As CheckBox
'Loop through each checkbox in Sheet1. Set it to color 6 if true, otherwise no color
For Each xChk In Sheets("Sheet1").CheckBoxes
If xChk.Value = 1 Then
Sheets("Sheet1").Range(xChk.LinkedCell).Interior.ColorIndex = 6
Else
Sheets("Sheet1").Range(xChk.LinkedCell).Interior.ColorIndex = xlNone
End If
Next
End Sub
我在这里完全假设,但是我可以想象您希望单击复选框时触发此宏。 有一个方便的Application.Caller
,它保存导致调用宏的对象的名称。 您可以将每个复选框的“分配宏..”设置为此新代码,然后可以使用application.caller
找出哪个复选框称为子例程/宏,并遵循相同的逻辑来切换其链接的单元格颜色:
Sub Change_Cell_Colour()
Dim xChk As CheckBox
'Who called this subroutine/macro?
Dim clickedCheckbox As String
clickedCheckbox = Application.Caller
'Lets check just this checkbox
Set xChk = Sheets("Sheet1").CheckBoxes(clickedCheckbox)
'toggle its color or colour if you are a neighbour
If xChk.Value = 1 Then
Sheets("Sheet1").Range(xChk.LinkedCell).Interior.ColorIndex = 6
Else
Sheets("Sheet1").Range(xChk.LinkedCell).Interior.ColorIndex = xlNone
End If
End Sub
根据是否选中复选框突出显示单元格
选择工作表并应用以下CF公式规则:
=A1=TRUE
ActiveWorksheet
不存在,并且由于您尚未在模块顶部指定Option Explicit
,因此VBA会愉快地将其视为现场Variant
变量。
除了,就是在现场创建的Variant
没有子类型,因此是Variant/Empty
。
而ActiveWorksheet.Cells
在语法上是成员调用,因此VBA理解它-因此ActiveWorksheet
必须是一个对象-但它是Variant/Empty
,因此是必需的对象 :除非ActiveWorksheet
是实际的Worksheet
对象引用,否则该调用是非法的。
在模块顶部指定Option Explicit
。 声明所有变量。
然后将ActiveWorksheet
更改为ActiveSheet
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.