Excel中实现鼠标指向哪个单元格那个单元格就变色

在工作薄中的任意工作表中添加两个窗体按钮控件,将指定其设置 分别指定为ChangeColor StopChange。其供示范之用
效果图:

模块中代码:

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
Sub  StopChange()
    
On   Error   Resume   Next
    
If   Not  blnStop  Then
        blnStop 
=   True
    
End   If
End Sub

Sub  ChangeColor()
    
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

转载于:https://www.cnblogs.com/wangminbai/archive/2008/02/20/1075319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值