0

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

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
4

1 回答 1

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
于 2013-05-07T01:03:11.450 回答