实现Excel对查找结果高亮突出显示,我们可以通过VBA自定义事件Workbook_SheetActivate来设置搜索结果单元格的背景色达到需求。
Dim isEnableFilter As Boolean Dim previousRange As Range Dim previousRangeColorIndex As Variant Private Sub Workbook_SheetActivate(ByVal Sh As Object) If isEnableFilter Then previousRange.Interior.ColorIndex = previousRangeColorIndex Set previousRange = Selection previousRangeColorIndex = Selection.Interior.ColorIndex Selection.Interior.ColorIndex = 4 End If End Sub Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If isEnableFilter Then previousRange.Interior.ColorIndex = previousRangeColorIndex Set previousRange = Target previousRangeColorIndex = Target.Interior.ColorIndex Target.Interior.ColorIndex = 4 End If End Sub Sub StartFilter() isEnableFilter = True Set previousRange = Selection previousRangeColorIndex = previousRange.Interior.ColorIndex End Sub Sub StopFilter() isEnableFilter = False previousRange.Interior.ColorIndex = previousRangeColorIndex End Sub
原文链接:https://www.zhihu.com/zvideo/1438632808906887168