[英]VBA - Highlight Cell With Checkbox
Some logic to my process: 我的流程的一些逻辑:
In column K on my worksheet I have inserted check boxes from cell K3 - K53 (this could become longer in the future) using the developer tab. 在工作表的K列中,我使用开发人员标签插入了单元格K3-K53中的复选框(将来可能会更长)。
I then associated the check box with the same cell it is placed in. 然后,我将复选框与放置该单元格的位置相关联。
I formatted the cells in this column by going to 'Format Cells', clicking on 'Custom' then typing in ';;;'. 我通过转到“设置单元格格式”,单击“自定义”,然后键入“ ;;;”来格式化此列中的单元格。 This was to HIDE the 'True/False' text from view. 这是为了隐藏 “ True / False”文本。
My next step is to change the cell colour based on the text. 我的下一步是根据文本更改单元格颜色。
Note: 注意:
I have searched through a few forums and combined some code samples from them all, so I will not be able to reference the sources exactly, but below is what I have so far: 我已经搜索了几个论坛,并结合了所有论坛的一些代码示例,因此我将无法准确地引用源,但是下面是到目前为止的内容:
Code: 码:
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
I keep getting an error on the line where I try to get the last row. 我在尝试获取最后一行的行上不断出现错误。
Code: 码:
lRow = ActiveWorksheet.Cells(Rows.Count, "B").End(xlUp).Row
Error: 错误:
Object Required 所需对象
I am not even sure if the code I have will solve my issue, so any help solving the main issue highlighting a cell based on the check box being checked or not , will be greatly appreciated. 我什至不确定我所拥有的代码是否可以解决我的问题,因此,对于解决主要问题的任何帮助( 基于是否选中复选框,突出显示一个单元格)都将不胜感激。
Here's a quick rewrite with LOTS of comments explaining: 这是快速重写的带有注释的批注,说明:
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
Here's the barebones version just to get it working 这是准系统版本,只是为了使其正常工作
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
I'm totally assuming here, but I would imagine you want this macro to fire when a checkbox is clicked. 我在这里完全假设,但是我可以想象您希望单击复选框时触发此宏。 There is a handy Application.Caller
that holds the name of the object that caused a macro to be called. 有一个方便的Application.Caller
,它保存导致调用宏的对象的名称。 You can set the "Assign Macro.." of each checkbox to this new code and then you can figure out which checkbox called the subroutine/macro using application.caller
and follow the same logic to toggle it's linked cell color: 您可以将每个复选框的“分配宏..”设置为此新代码,然后可以使用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
highlighting a cell based on the check box being checked or not 根据是否选中复选框突出显示单元格
Select the sheet and apply a CF formula rule of: 选择工作表并应用以下CF公式规则:
=A1=TRUE
ActiveWorksheet
doesn't exist, and because you haven't specified Option Explicit
at the top of your module, VBA happily considers it an on-the-spot Variant
variable. ActiveWorksheet
不存在,并且由于您尚未在模块顶部指定Option Explicit
,因此VBA会愉快地将其视为现场Variant
变量。
Except, a Variant
created on-the-spot doesn't have a subtype, so it's Variant/Empty
. 除了,就是在现场创建的Variant
没有子类型,因此是Variant/Empty
。
And ActiveWorksheet.Cells
being syntactically a member call, VBA understands it as such - so ActiveWorksheet
must therefore be an object - but it's a Variant/Empty
, hence, object required : the call is illegal unless ActiveWorksheet
is an actual Worksheet
object reference. 而ActiveWorksheet.Cells
在语法上是成员调用,因此VBA理解它-因此ActiveWorksheet
必须是一个对象-但它是Variant/Empty
,因此是必需的对象 :除非ActiveWorksheet
是实际的Worksheet
对象引用,否则该调用是非法的。
Specify Option Explicit
at the top of the module. 在模块顶部指定Option Explicit
。 Declare all variables. 声明所有变量。
Then change ActiveWorksheet
for ActiveSheet
. 然后将ActiveWorksheet
更改为ActiveSheet
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.