如何使用 Activex 控件按钮更改一系列单元格的颜色?

How to change color in a range of cells using an Axtive X control button?

我正在尝试使用 Active X 控件按钮在单击该按钮时更改一系列单元格的背景(填充)颜色,然后在再次单击该按钮时将其更改回其原始颜色。我的代码有 returns 个错误。我正在寻找一个简单的解决方案。

基本思路:

   Private Sub CommandButton3_Click()
    If Intersect(target, Range("M3, 03:Z3")) Is Nothing Then Exit Sub
        If target.Interior.ColorIndex = RGB(252, 228, 214) Then
            target.Interior.ColorIndex = 6
        ElseIf target.Interior.ColorIndex = 6 Then
            target.Interior.ColorIndex = RGB(252, 228, 214)
        End If
    End Sub

试试这个:

Private Sub CommandButton3_Click()
    Dim c As Range
    With Me.Range("M3,O3:Z3")
        Set c = .Cells(1)  'assuming all cells in the range of interest have the same color?
        If c.Interior.Color = RGB(252, 228, 214) Then
            .Interior.ColorIndex = 6
            Me.Range("J3").Value = "Some text"
        ElseIf c.Interior.ColorIndex = 6 Then
            .Interior.Color = RGB(252, 228, 214)
            Me.Range("J3").ClearContents
        End If
    End With
End Sub