Excel - 删除特定边框的更有效方法

Excel - More efficient way to remove specific borders

如何让下面的代码更有效率,目前它大约需要 30 秒才能完成

该代码将清除选区内具有白色背景的所有单元格,然后清除选区内的所有对角线边框。

只有带有白色背景代码的透明单元格,它会立即完成,但一旦我添加删除边框代码,它就会变慢。

Sub ADULTClearOnly()
Set sh2 = ThisWorkbook.Worksheets("ADULT Sign On Sheet")

sh2.Select
Cells.Range("B1:F756").Select
    For Each Cell In Selection
    If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
    Cell.Value = ""
    End If
    Next
    Cells.Range("G1:AO757").Select
    For Each Cell In Selection
    If Cell.Borders(xlDiagonalUp).LineStyle = xlDiagonalUp Then
    Cell.Borders(xlDiagonalUp).LineStyle = xlNone
    End If
    Next

    End Sub

您的代码中存在几个问题:

  1. 您不需要也不应该Select执行这些操作
  2. 你的逻辑有问题:xlDiagonalUp 不是 LineStyle
  3. 因为您将任何对角向上边框设置为 none,所以您不需要迭代范围,一步完成

所以,替换

Cells.Range("G1:AO757").Select
For Each Cell In Selection
    If Cell.Borders(xlDiagonalUp).LineStyle = xlDiagonalUp Then
        Cell.Borders(xlDiagonalUp).LineStyle = xlNone
    End If
Next

sh2.Range("G1:AO757").Borders(xlDiagonalUp).LineStyle = xlNone

同样,

sh2.Select
Cells.Range("B1:F756").Select
For Each Cell In Selection
    If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
        Cell.Value = ""
    End If
Next

可以减少到

For Each Cell In sh2.Range("B1:F756")
    If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
        Cell.ClearContents
    End If
Next