遍历选定的单元格,检查边框并更改颜色
Loop through selected cells, check for borders and change colour
我正在为工具栏创建一个按钮,用于在选定区域内将颜色从白色背景、黑色字体更改为白色字体黑色背景。 (将用于会计中的损益表、资产负债表等表格)。
但我还需要按钮中的一些功能,它可以查看所选的单元格,找到任何现有的边框并将它们变成白色。可能通过使用布尔值或其他方法检查背景颜色是否为黑色,然后将现有边框变为白色。我不需要制作任何新边框,只需反转现有边框的颜色即可。
这是我已经拥有的,但它只会使所有边框变白:
Dim Background As Boolean
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
With Selection.Borders
For Each cel In selectedRange.Cells
If cel.Borders(xlEdgeTop).LineStyle <> xlLineStyleNone Then
.Color = RGB(255, 255, 255)
End If
If cel.Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
.Color = RGB(255, 255, 255)
End If
Next cel
End With
希望你能帮助我:)
您将颜色应用于 Selection.Borders
集合中的每个边框,因为那是 With
变量。只需设置 cel.Borders(xlEdgeTop)
的颜色
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
For Each cel In selectedRange.Cells
With cel.Borders
If .Item(xlEdgeTop).LineStyle <> xlLineStyleNone Then
.Item(xlEdgeTop).Color = vbWhite
End If
If .Item(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
.Item(xlEdgeBottom).Color = vbWhite
End If
End With
Next cel
您还可以使用两个 With
块:With cel.Borders(xlEdgeTop)
和 With cel.Borders(xlEdgeBottom)
,然后只使用 .LineStyle
和 .Color
。您也可以完全跳过 With
块,因为它实际上并没有节省太多(cel.Borders
-> .Item
)。
我正在为工具栏创建一个按钮,用于在选定区域内将颜色从白色背景、黑色字体更改为白色字体黑色背景。 (将用于会计中的损益表、资产负债表等表格)。
但我还需要按钮中的一些功能,它可以查看所选的单元格,找到任何现有的边框并将它们变成白色。可能通过使用布尔值或其他方法检查背景颜色是否为黑色,然后将现有边框变为白色。我不需要制作任何新边框,只需反转现有边框的颜色即可。
这是我已经拥有的,但它只会使所有边框变白:
Dim Background As Boolean
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
With Selection.Borders
For Each cel In selectedRange.Cells
If cel.Borders(xlEdgeTop).LineStyle <> xlLineStyleNone Then
.Color = RGB(255, 255, 255)
End If
If cel.Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
.Color = RGB(255, 255, 255)
End If
Next cel
End With
希望你能帮助我:)
您将颜色应用于 Selection.Borders
集合中的每个边框,因为那是 With
变量。只需设置 cel.Borders(xlEdgeTop)
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
For Each cel In selectedRange.Cells
With cel.Borders
If .Item(xlEdgeTop).LineStyle <> xlLineStyleNone Then
.Item(xlEdgeTop).Color = vbWhite
End If
If .Item(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
.Item(xlEdgeBottom).Color = vbWhite
End If
End With
Next cel
您还可以使用两个 With
块:With cel.Borders(xlEdgeTop)
和 With cel.Borders(xlEdgeBottom)
,然后只使用 .LineStyle
和 .Color
。您也可以完全跳过 With
块,因为它实际上并没有节省太多(cel.Borders
-> .Item
)。