VBA-使用复选框突出显示单元格

时间:2018-06-28 15:21:29

标签: excel vba excel-vba

我的流程有些逻辑:

在工作表的K列中,我使用“开发人员”标签插入了单元格K3-K53中的复选框(将来可能会更长)。

然后,我将复选框与放置该单元格的单元格相关联。

我通过转到“设置单元格格式”,单击“自定义”,然后键入“ ;;;”来格式化此列中的单元格。这是为了从视图中隐藏“真/假”文本。

下一步是根据文本更改单元格颜色。

注意:

我已经搜索了几个论坛,并结合了所有论坛中的一些代码示例,因此我将无法准确地引用源代码,但是下面是到目前为止的内容:

代码:

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

错误:

  

需要对象

我什至不确定我所拥有的代码是否能解决我的问题,因此对解决主要问题的任何帮助根据是否选中复选框突出显示单元格,将不胜感激。

3 个答案:

答案 0 :(得分:2)

这里有很多评论,说明如下:

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

答案 1 :(得分:2)

  

根据是否选中复选框突出显示单元格

选择工作表并应用以下CF公式规则:

=A1=TRUE

答案 2 :(得分:2)

ActiveWorksheet不存在,并且由于您尚未在模块顶部指定Option Explicit,因此VBA会愉快地将其视为现场Variant变量。 / p>

除了,即场创建的Variant没有子类型,所以它是Variant/Empty

并且ActiveWorksheet.Cells在语法上是成员调用,VBA这样理解它-因此ActiveWorksheet必须是一个对象-但它是Variant/Empty,因此,需要对象< / em>:除非ActiveWorksheet是实际的Worksheet对象引用,否则该调用是非法的。

在模块顶部指定Option Explicit。声明所有变量。

然后将ActiveWorksheet更改为ActiveSheet