在excel2007中实现在选中单元格后使其改变背景颜色,在鼠标移开后恢复原有颜色?
2个回答
展开全部
按Alt+F11,双击sheet1,输入以下代码:
Private Sub Worksheet_Activate()
Dim LngCurPos As POINTAPI
Dim NewRange As Range
On Error Resume Next
blnStop = False
If ChangeOn Then
Exit Sub
Else
ChangeOn = True
End If
Do
If blnStop = True Then Exit Do
GetCursorPos LngCurPos
On Error Resume Next
Set NewRange = ActiveWindow.RangeFromPoint(LngCurPos.X, LngCurPos.Y)
If Err <> 0 Then
OldRange.Interior.ColorIndex = OldColorIndex
Else
If NewRange.Address <> OldRange.Address Then
If OldRange Is Nothing Then
Else
OldRange.Interior.ColorIndex = OldColorIndex
End If
OldColorIndex = NewRange.Interior.ColorIndex
NewRange.Interior.ColorIndex = 3
End If
Set OldRange = NewRange
End If
On Error GoTo 0
DoEvents
Loop
ChangeOn = False
End Sub
=======================================================================
然后选择 插入-模块,在模块里面输入以下代码:
Option Explicit
Declare Function GetCursorPos _
Lib "user32" ( _
lpPoint As POINTAPI) _
As Long
Type POINTAPI
X As Long
Y As Long
End Type
Dim ChangeOn As Boolean
Dim OldRange As Range
Dim OldColorIndex As Integer
Dim blnStop As Boolean
最后,返回Excel表格,切换到Sheet2再回到Sheet1,你会发现效果。
Private Sub Worksheet_Activate()
Dim LngCurPos As POINTAPI
Dim NewRange As Range
On Error Resume Next
blnStop = False
If ChangeOn Then
Exit Sub
Else
ChangeOn = True
End If
Do
If blnStop = True Then Exit Do
GetCursorPos LngCurPos
On Error Resume Next
Set NewRange = ActiveWindow.RangeFromPoint(LngCurPos.X, LngCurPos.Y)
If Err <> 0 Then
OldRange.Interior.ColorIndex = OldColorIndex
Else
If NewRange.Address <> OldRange.Address Then
If OldRange Is Nothing Then
Else
OldRange.Interior.ColorIndex = OldColorIndex
End If
OldColorIndex = NewRange.Interior.ColorIndex
NewRange.Interior.ColorIndex = 3
End If
Set OldRange = NewRange
End If
On Error GoTo 0
DoEvents
Loop
ChangeOn = False
End Sub
=======================================================================
然后选择 插入-模块,在模块里面输入以下代码:
Option Explicit
Declare Function GetCursorPos _
Lib "user32" ( _
lpPoint As POINTAPI) _
As Long
Type POINTAPI
X As Long
Y As Long
End Type
Dim ChangeOn As Boolean
Dim OldRange As Range
Dim OldColorIndex As Integer
Dim blnStop As Boolean
最后,返回Excel表格,切换到Sheet2再回到Sheet1,你会发现效果。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-10-24
展开全部
这个内置功能无法设置,可以通过VBA写一些工作表事件代码来实现
总的来说,有点画蛇添足,并具可操作性不强.
总的来说,有点画蛇添足,并具可操作性不强.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询