答案1
如果你对细黑边框不介意,那么条件格式就可以了。如果你真的想要一个厚的黑色边框,您将需要 VBA 事件代码,因为您无法在条件格式边框属性中修改边框的粗细。
要输入此事件触发的宏,请右键单击工作表选项卡。从右键单击下拉菜单中选择“查看代码”。然后将下面的代码粘贴到打开的窗口中。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dataRange As Range
Dim v As Variant, I As Long
Set dataRange = Range(Cells(2, 2), Cells(100, 10)) 'change as needed
If Not Intersect(Target, dataRange) Is Nothing Then
With dataRange
v = .Columns(9)
For I = 1 To UBound(v) - 1
If v(I, 1) <> v(I + 1, 1) Then
With .Rows(I).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = vbBlack
.Weight = xlThick
End With
Else
.Rows(I).Borders(xlEdgeBottom).LineStyle = xlNone 'or whatever you want for the default border
End If
Next I
End With
End If
End Sub
编辑:拥有一个独立的宏,而不是事件触发的宏
进入常规模块
然后您可以从另一个宏调用它,或者用按钮触发它,或者手动启动它
Option Explicit
Sub LineIT()
Dim WS As Worksheet
Dim dataRange As Range
Dim v As Variant, I As Long
Set WS = ThisWorkbook.Worksheets("Sheet4") 'or whatever workbook and worksheet the table you which to process is located
With WS
Set dataRange = Range(.Cells(2, 1), Cells(.Rows.Count, 10).End(xlUp)) 'change as needed
With dataRange
v = .Columns(10)
For I = 1 To UBound(v) - 1
If v(I, 1) <> v(I + 1, 1) Then
With .Rows(I).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = vbBlack
.Weight = xlThick
End With
Else
.Rows(I).Borders(xlEdgeBottom).LineStyle = xlNone 'or whatever you want for the default border
End If
Next I
End With
End With
End Sub