百度空间 | 百度首页 
               
 
查看文章
 
Excel中鼠标指向单元格改变颜色
2008-01-20 15:23

    

 Excel中鼠标指向单元格改变颜色

    

 Excel2002及其以后版本

    

  本示例运用API函数来获得鼠标指向单元格的地址进而更改其单元格颜色。Code By wangminbai)

       在工作薄中的任意工作表中添加两个窗体按钮控件,将指定其设置 分别指定为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

类别:excel杂记 | 添加到搜藏 | 浏览() | 评论 (3)
 
最近读者:
 
网友评论:
1
2008-05-03 13:13 | 回复
这个真好玩,谢谢了,可以借鉴用一下!
 
2
2008-05-28 15:49 | 回复
引用了博主的代码,如觉不妥,请告知。 http://www.excelpx.com/dispbbs.asp?boardid=5&id=51429&star=1#51429
 
3
2008-05-28 18:55 | 回复
没关系的,随意~~~~~~~~~~~
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu