簡體   English   中英

VBA-使用復選框突出顯示單元格

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM