希望能帮到你。该代码在你进行选择单元格时自动执行,包括你修改过后,也会自动填充相应背景色及边框。如有需要修改,继续追问
Option Explicit
Public i As Integer
Public j As Integer
Public borders_colors As Integer
Public backgroup_colors As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If i <> 0 And j <> 0 Then
If Cells(i, j) <> "" Then
If IsNumeric(Cells(i, j)) Then
If Cells(i, j) > 0 Then '设置大于0时
backgroup_colors = 26 '数字表示填充颜色,可自行修改
borders_colors = 16 '数字表示边框颜色,可自行修改
Call set_backgroup
Call set_borders
End If '设置小于0时
If Cells(i, j) < 0 Then
backgroup_colors = 41 '数字表示填充颜色,可自行修改
borders_colors = 16 '数字表示边框颜色,可自行修改
Call set_backgroup
Call set_borders
End If
Else '以下还原为白底黑字
Cells(i, j).Interior.ColorIndex = 0
Cells(i, j).Borders(xlInsideVertical).LineStyle = xlNone
Cells(i, j).Borders(xlInsideHorizontal).LineStyle = xlNone
End If
End If
End If
i = Target.Row
j = Target.Column
End Sub
Private Sub set_borders()
With Cells(i, j).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = borders_colors
.TintAndShade = 0
.Weight = xlThin
End With
With Cells(i, j).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = borders_colors
.TintAndShade = 0
.Weight = xlThin
End With
With Cells(i, j).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = borders_colors
.TintAndShade = 0
.Weight = xlThin
End With
With Cells(i, j).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = borders_colors
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
Private Sub set_backgroup()
Cells(i, j).Interior.ColorIndex = backgroup_colors '数字表示填充颜色,可自行修改
End Sub
不太理解你的要求,什么是“目前所选区块”,不选中就不起作用吗?
第一种情况用条件格式
第二种情况要用vba,但数据量大时,速度难说
这根本不需要用VBA,用条件格式不就行了。