阅: 106 | 回: 2
发表于2025/8/3 16:20:09
楼主

-
积分:2
-
财富值:2.0
-
身份:普通用户
' 类模块: clsEventClassModule
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If TypeOf Sh Is Worksheet Then
Call HighlightActiveCellRowAndColumn(Sh, Target)
End If
End Sub
' 标准模块
Option Explicit
Public EventClassModule As New clsEventClassModule
Sub Auto_Open()
Set EventClassModule.App = Application
End Sub
Sub Auto_Close()
Set EventClassModule = Nothing
End Sub
Public Sub HighlightActiveCellRowAndColumn(ws As Worksheet, Target As Range)
On Error GoTo l_err
' 清除之前的高亮显示
ws.Cells.Interior.ColorIndex = xlNone
' 设置行范围并高亮显示
With ws.Rows(Target.Row)
.Interior.Color = RGB(255, 255, 0) ' 黄色
End With
' 设置列范围并高亮显示
With ws.Columns(Target.Column)
.Interior.Color = RGB(255, 255, 0) ' 黄色
End With
Exit Sub
l_err:
MsgBox "发生错误:" & Err.Description, vbCritical
End Sub


Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
End Sub
Private Sub App_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If TypeOf Sh Is Worksheet Then
Call HighlightActiveCellRowAndColumn(Sh, Target)
End If
End Sub
' 标准模块
Option Explicit
Public EventClassModule As New clsEventClassModule
Sub Auto_Open()
Set EventClassModule.App = Application
End Sub
Sub Auto_Close()
Set EventClassModule = Nothing
End Sub
Public Sub HighlightActiveCellRowAndColumn(ws As Worksheet, Target As Range)
On Error GoTo l_err
' 清除之前的高亮显示
ws.Cells.Interior.ColorIndex = xlNone
' 设置行范围并高亮显示
With ws.Rows(Target.Row)
.Interior.Color = RGB(255, 255, 0) ' 黄色
End With
' 设置列范围并高亮显示
With ws.Columns(Target.Column)
.Interior.Color = RGB(255, 255, 0) ' 黄色
End With
Exit Sub
l_err:
MsgBox "发生错误:" & Err.Description, vbCritical
End Sub


我的个性签名