简体   繁体   中英

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.

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.

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. 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:

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:

=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.

Except, a Variant created on-the-spot doesn't have a subtype, so it's 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.

Specify Option Explicit at the top of the module. Declare all variables.

Then change ActiveWorksheet for ActiveSheet .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM