【问题标题】:Loop through selected cells, check for borders and change colour循环遍历选定的单元格,检查边框并更改颜色
【发布时间】:2016-11-11 09:12:36
【问题描述】:

我正在为工具栏创建一个按钮,用于在选定区域内将颜色从白色背景、黑色字体更改为白色字体黑色背景。 (将用于会计中的损益表、资产负债表等表格)。

但我还需要按钮中的一些功能来查看选定的单元格,找到任何现有的边框并将它们变成白色。也许通过使用布尔值或其他东西检查背景颜色是否为黑色,然后将现有边框变为白色。我不需要制作任何新边框,只需反转现有边框的颜色即可。

这是我已经拥有的,但它只是让所有边框变白:

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

希望你能帮助我:)

【问题讨论】:

  • 到目前为止你尝试过什么?您可以轻松检查单元格的背景颜色。定位现有边界是什么意思?您是否有一些隐藏的边框(即 linestyle = xlNone)?如果是这样,我认为将它们的颜色设置为白色不应改变可见性。
  • 感谢您的回复!是的,我已经进行了背景检查,效果很好,但我只能找到一种方法让所有边框都变白。猜猜我的斗争是找到一种方法来查看选定的单元格,检查是否有边界并将它们涂成白色。资产负债表等总是在白色背景上制作,因此“现有边框”将是这样的:google.dk/… 没有隐藏的边框,全部可见

标签: excel vba colors border


【解决方案1】:

您将颜色应用于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)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多