我有一个Excel表单,其边框为黑色。我想将其更改为其他颜色。我尝试了以下代码:
ActiveSheet.UsedRange.Borders.Color = RGB(255, 0, 0)
它将所有单元格(包括那些没有边框的单元格)的边框更改为红色。这不是我想要的。我希望黑色的边框变成红色,而看不见的边框保持不可见。有办法吗?
答案 0 :(得分:2)
使用FindFormat
和ReplaceFormat
属性的另一种处理方式。
<!-- It is better to disable the button if it is subscribed-->
<button class="btn btn-success" (click)="subscribe(category.categoryId)">{{ category.isSubscribed?'Subscribed':'Subscribe'}}</button>
涉及小的循环以进行适当的XLBordersIndex
枚举。
注意,不清除Sub BordersReplace()
With ThisWorkbook.Sheets(1)
For X = xlEdgeLeft To xlEdgeRight
With Application.FindFormat.Borders(X)
.Color = 0
End With
With Application.ReplaceFormat.Borders(X)
.Color = 255
End With
.Cells.Replace What:="", Replacement:="", searchformat:=True, ReplaceFormat:=True
Application.FindFormat.Clear
Application.ReplaceFormat.Clear
Next X
End With
End Sub
和FindFormat
将使Excel继续使用第一种使用的格式,因此为什么ReplaceFormat
是必不可少的。
我自己对为什么在边框上应用了所有边缘的情况下为什么不能在单元格上使用感到有些困惑。为此,请使用.Clear
答案 1 :(得分:1)
感谢Mikku的投入,我得到了下面的代码。
Sub change_border_color()
'change the color of existing borders
Dim cell As Range
Application.ScreenUpdating = False
For Each cell In ActiveSheet.UsedRange
If cell.Borders(xlEdgeLeft).LineStyle = 1 Then
cell.Borders(xlEdgeLeft).Color = RGB(0, 0, 255)
End If
If cell.Borders(xlEdgeTop).LineStyle = 1 Then
cell.Borders(xlEdgeTop).Color = RGB(0, 0, 255)
End If
If cell.Borders(xlEdgeBottom).LineStyle = 1 Then
cell.Borders(xlEdgeBottom).Color = RGB(0, 0, 255)
End If
If cell.Borders(xlEdgeRight).LineStyle = 1 Then
cell.Borders(xlEdgeRight).Color = RGB(0, 0, 255)
End If
Next
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
使用此:
循环将正常工作。当前,您要设置完整的范围并更改其边框,只需要对具有任何值的单元格进行设置即可。
如果单元格当前具有任何边框,则此循环将边框红色着色。
For Each cel In ActiveSheet.UsedRange
If Not cel.Borders(xlEdgeLeft).LineStyle = 0 Then
cel.Borders.Color = RGB(255, 0, 0)
End If
Next
此循环将为cel具有某些值的边框上色。
For Each cel In ActiveSheet.UsedRange
If Not cel.Value = "" Then
cel.Borders.Color = RGB(255, 0, 0)
End If
Next