如何更改单元格不再处于活动状态时的颜色属性

时间:2013-05-06 23:33:16

标签: vba excel-vba excel

我有一个工作表事件宏,在单击超链接公式时触发 - 它“突出显示”(更改单元格内部颜色)超链接目标单元格,以便最终用户更容易看到它。当用户不再需要时,如何将目标单元格更改为“未突出显示”(即将单元格内部颜色更改为白色)?也就是说,我已经将“高位”打开,我如何将其“关闭”?下面的代码突出显示“on”:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ControlPoint As String
Dim RowVar As Integer
Dim Destination As String

If InStr(1, Target.Cells(1, 1).Formula, "HYPERLINK") = 0 Then Exit Sub

ControlPoint = Target.Value

RowVar = Application.WorksheetFunction _
            .Match(ControlPoint, _
            Sheets("Control Point Log").Range("c1:c700"), 0)

Destination = "C" & RowVar

Sheets("Control Point Log").Range(Destination).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark2
        .TintAndShade = -9.99786370433668E-02
        .PatternTintAndShade = 0
    End With

End Sub

1 个答案:

答案 0 :(得分:0)

这一切都在工作表模块中,用于要捕获事件的工作表。注意:您可能需要将工作表代码从“Sheet1”修改为工作表的代号。

我简化了你的突出显示只是改变填充颜色:我不知道你的“默认”格式应该是什么样的。

编辑:现在突出显示第二张

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const CLEAR_AFTER_SEC As Long = 4
Dim f As Range, trg As Range, addr As String
Dim sht As Worksheet
    Set trg = Target.Cells(1)

    If InStr(1, trg.Formula, "HYPERLINK") = 0 Then Exit Sub

    Set sht = ThisWorkbook.Sheets("Sheet2") 'edit here for your sheet name
    Set f = sht.Range("C1:C700").Find(trg.Value, , xlValues, xlWhole)

    If Not f Is Nothing Then
        addr = f.Address(False, False)
        Highlight addr

        f.Parent.Activate
        Application.Goto f

        Application.OnTime Now + TimeSerial(0, 0, CLEAR_AFTER_SEC), _
                  "'Sheet1.Highlight """ & addr & """, True'"
    End If

End Sub

Public Sub Highlight(rng As String, Optional reverse As Boolean = False)
    'edit sheet name to suit...
    With ThisWorkbook.Sheets("Sheet2").Range(rng).Interior
        .Color = IIf(reverse, vbWhite, vbRed)
    End With
End Sub