在使用Excel处理数据的时候,数据的输入是最关键的部分。
如果您在输入数据的时候,尤其是数据量比较大的情况,往往会因为无法直观的判断单元格所在的行,导致输入的数据输入到其它的行!
那么,有没有一种办法,能让我们实现,一旦选择了哪个单元格,该单元格的所在的行、列或单元格的颜色会发生改变,这样,我们就能直观的看出,数据应该输入到哪行了。
根据目前的情况,要实现这样的效果,只能使用VBA来实现,下面是方法,请君过目。
如下图,这是正常情况下的效果,当我们选择一个单元格后,只有这个单元格边框颜色有所变化,该行的其它单元格,都未发生任何变化。
现在,我们要做的就是,如下图一样,一旦选择了某个单元格,那么,该单元格所在的行、列或单元格,颜色发生改变,以方便我们录入数据。
实现的方法是,在工作表名称(如:Sheet1)上点击右键,在弹出的快捷菜单中选择“查看代码”,如下图!
接下来,会弹出VBA代码编写窗口,如下图!
上图中,在Sheet1上点击右键,选择“查看代码”,弹出代码编写窗口。
现在,我们要做的就是输入VBA功能代码了,代码如上图!记住,先选择好Worksheet对象名称和SelectionChange事件名称,然后在窗口中输入如上图右边的那些代码。
为方便大家学习,下面粘贴出源代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Rows().Interior.ColorIndex = 0
x = ActiveCell.Row
Rows(x).Interior.ColorIndex = 9
End Sub
部分代码解释:Rows(x).Interior.ColorIndex = 9,其中的9,指的是颜色的代码,您可以从1到几百中选择一个颜色的值,此处可修改。至于其它地方,如果您不懂,千万别修改。
附VBA颜色代码,你就可以根据自己的爱好进行自己的调整。
下面是空心根据一高手心得整理,要谢就谢高手。原文来自
一、行列都变色,可填充,不能复制,可多行多列变色、可不断变化各种色彩(推荐使用)
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
Cells.FormatConditions.Delete
iColor = Int(50 * Rnd() + 2)
With Target.EntireRow.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = iColor
End With
With Target.EntireColumn.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = iColor
End With
End Sub
二、行变色,可填充,不能复制,可多行变色、可不断变化各种色彩(推荐使用)
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
Cells.FormatConditions.Delete
With Target.EntireRow.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(50 * Rnd() + 2)
End With
End Sub
三、列变色,可填充,不能复制,可多列变色、可不断变化各种色彩(推荐使用)
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
Cells.FormatConditions.Delete
With Target.EntireColumn.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(50 * Rnd() + 2)
End With
End Sub
四、单元格变色,可填充,不能复制,可多格变色、可不断变化各种色彩(推荐使用)
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
Cells.FormatConditions.Delete
With Target.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(50 * Rnd() + 2)
End With
End Sub
五、行变色,可填充,不能复制,可多行变色
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
Cells.FormatConditions.Delete
With Target.EntireRow.FormatConditions
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = 7
End With
End Sub
六、行、列都变色,不可填充,不能复制,只能单行单列变色,很好用。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
Rows(Target.Row).Interior.ColorIndex = 6
Columns(Target.Column).Interior.ColorIndex = 6
Cells(Target.Row, Target.Column).Interior.ColorIndex = 33
End Sub
七、行变色,不可填充,不能复制,只能单行变色
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
Rows(Target.Row).Interior.ColorIndex = 6
Cells(Target.Row, Target.Column).Interior.ColorIndex = 33
End Sub
八、列变色,不可填充,不能复制,只能单列变色
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
Columns(Target.Column).Interior.ColorIndex = 6
Cells(Target.Row, Target.Column).Interior.ColorIndex = 33
End Sub下载本文