立即注册 登录
西班牙华人网 西华论坛 返回首页

lilia7的个人空间 https://xihua.es/?1514 [收藏] [复制] [分享] [RSS]

日志

自动为当前单元格所在行和列加上颜色

已有 125 次阅读2009-6-23 15:17 |个人分类:资料分享|

当光标定位在工作表中某单元格中时,Excel自动为该单元格或者该单元格所在的行或列加上颜色,这个技巧可用于在工作表中突出显示当前单元格及其所在的行和列,以强调数据,并方便阅读。下面介绍了实现此功能的几种方法。
 
使用条件格式
下面将使用条件格式实现当前单元格和当前单元所在行或列突出显示的效果。在公式中使用了Cell函数,并将ScreenUpdation属性的值设置为True以实现屏幕更新。
在ThisWorkbook模块中输入如下代码:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Application.ScreenUpdating = True
End Sub
当然,如果只想在工作表中实现突出显示的效果,将Application.ScreenUpdating = True代码输入到Worksheet_SelectionChange事件中。
 
接下来,在Excel中选择菜单“格式——条件格式”命令,弹出“条件格式”对话框。在“条件”下拉框中选择“公式”并在右侧中输入下面相应的公式,然后点击“格式”按钮,设置相应的格式,完成后,按“确定”按钮。
下面是相应的公式及其作用:
(1)公式“=CELL("address")=ADDRESS(ROW(),COLUMN())”,突出活动单元格
 
(2)公式“=CELL("row")=ROW()”,突出单元格所在行
 
(3)下面的公式突出到当前单元格为止的相应行和列,呈反L形。
=OR(AND(CELL("row")=ROW(),CELL("col")+1>COLUMN()),AND(CELL("col")=COLUMN(),CELL("row")+1>ROW()))
(4)在“条件格式”对话框中对所选单元格区域设置两个如下所列的公式条件,将呈反L形突出显示到当前单元格为止的相应行和列,且当前单元格背景色改变、字体加粗显示,如下图4所示。
=CELL("address")=ADDRESS(ROW(),COLUMN())
=OR(AND(CELL("row")=ROW(),CELL("col")+1>COLUMN()),AND(CELL("col")=COLUMN(),CELL("row")+1>ROW()))
 
使用VBA代码
(1) 突出显示至当前单元格所在的行和列,呈反L形。在需要设置此功能的工作表模块中输入下面的代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim iColor As Integer
  '注:如果工作表中有想要保留的条件格式,则不要使用本程序
  '忽略用户选择单元格区域时可能产生的错误
  On Error Resume Next
  iColor = Target.Interior.ColorIndex
  If iColor < 0 Then
    iColor = 36
  Else
     iColor = iColor + 1
  End If
  '避免字体颜色与突出色相同
  If iColor = Target.Font.ColorIndex Then iColor = iColor + 1
  Cells.FormatConditions.Delete
  '水平突出色
  With Range("A" & Target.Row, Target.Address)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
  End With
  '垂直突出色
  With Range(Target.Offset(1 - Target.Row, 0).Address & ":" & _
     Target.Offset(-1, 0).Address)
     .FormatConditions.Add Type:=2, Formula1:="TRUE"
     .FormatConditions(1).Interior.ColorIndex = iColor
  End With
End Sub
注意,此代码运行后,将清除所在工作表中含有的条件格式。示例文档见 用颜色自动突出显示当前单元格行列1.xls。
(2) 突出显示当前单元格所在的行和列。在需要设置此功能的工作表模块中输入下面的代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  '可带条件格式,但不能复制/剪切/粘贴操作
  With Target.Parent
    .Cells.Interior.ColorIndex = 0
    .Columns(Target.Column).Cells.Interior.ColorIndex = 35
    .Rows(Target.Row).Cells.Interior.ColorIndex = 35
  End With
End Sub
注意,此代码运行后,在当前工作表中不能进行复制、剪切和粘贴功能。示例文档见 用颜色自动突出显示当前单元格行列2.xls。
(3) 突出显示当前单元格所在的行和列。在需要设置此功能的工作表模块中输入下面的代码:
Private Sub Worksheet_Change(ByVal target As Range)
  If Application.CutCopyMode <> False Then
    Application.CutCopyMode = False
    Call ColorBand(target)
  End If
End Sub
‘- - - - - - - - - - - - - - - - - - - - -
Private Sub Worksheet_SelectionChange(ByVal target As Range)
  If Application.CutCopyMode = False Then
    Call ColorBand(target)
  Else
    Exit Sub
  End If
End Sub
‘- - - - - - - - - - - - - - - - - - - - -
Private Sub ColorBand(ByVal rngTarget As Range)
  With rngTarget.Parent
    .Cells.Interior.ColorIndex = 0
    .Columns(rngTarget.Column).Cells.Interior.ColorIndex = 35
    .Rows(rngTarget.Row).Cells.Interior.ColorIndex = 35
  End With
End Sub
此代码不会影响复制、剪切和粘贴功能。示例文档见 用颜色自动突出显示当前单元格行列3.xls。

(4) 在工作表中有一个复选框,用来决定是否突出显示当前单元格所在的行和列。即当您选中该复选框后,将突出显示当前单元格所在的行和列。在需要设置此功能的工作表模块中输入下面的代码:
Private Sub CheckBox1_Change()
  If Me.CheckBox1.Value = False Then
    Me.Cells.Interior.ColorIndex = 0
  End If
End Sub
‘- - - - - - - - - - - - - - - - - - - - -
Private Sub Worksheet_Change(ByVal target As Range)
  If Me.CheckBox1.Value = True Then
    If Application.CutCopyMode <> False Then
      Application.CutCopyMode = False
      Call ColorBand(target)
    End If
  End If
End Sub
‘- - - - - - - - - - - - - - - - - - - - -
Private Sub Worksheet_SelectionChange(ByVal target As Range)
  If Me.CheckBox1.Value = True Then
    If Application.CutCopyMode = False Then
      Call ColorBand(target)
    Else
      Exit Sub
    End If
  End If
End Sub
‘- - - - - - - - - - - - - - - - - - - - -
Private Sub ColorBand(ByVal rngTarget As Range)
  With rngTarget.Parent
    .Cells.Interior.ColorIndex = 0
    .Columns(rngTarget.Column).Cells.Interior.ColorIndex = 35
    .Rows(rngTarget.Row).Cells.Interior.ColorIndex = 35
  End With
End Sub
示例文档见 用颜色自动突出显示当前单元格行列4.xls。

By fanjy in 2006-10-27
这位大哥太强了~~

今天K问我这个怎么弄,于是网上搜了一下,虽然资料是找到了,但是宏咱不会用啊- - 不管怎么样,先保存下来以备后用~~


路过

鸡蛋

鲜花

握手

雷人

全部作者的其他最新日志

发表评论 评论 (1 个评论)

回复 gllnydnx 2009-6-27 01:17
看得我眼花

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关于我们|广告服务|免责声明|小黑屋|友情链接|Archiver|联系我们|手机版|西班牙华人网 西华论坛 ( 蜀ICP备05006459号 )

GMT+2, 2024-5-9 06:24 , Processed in 0.007017 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

返回顶部