Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance Excel-删除特定边框的更有效方法_Performance_Excel_Border_Vba - Fatal编程技术网

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

Performance Excel-删除特定边框的更有效方法,performance,excel,border,vba,Performance,Excel,Border,Vba,我怎样才能使下面的代码更有效率,目前大约需要30秒才能完成 该代码将清除选定区域内具有白色背景的所有单元格,然后清除选定区域内的所有对角线边框 只有带有白色背景代码的透明单元格,它会立即完成,但当我添加删除边框代码时,它就变慢了 Sub ADULTClearOnly() Set sh2 = ThisWorkbook.Worksheets("ADULT Sign On Sheet") sh2.Select Cells.Range("B1:F756").Select For Each Ce

我怎样才能使下面的代码更有效率,目前大约需要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

您的代码中有几个问题:

  • 您不需要也不应该选择执行这些操作
  • 您的逻辑有缺陷:
    xlDiagonalUp
    不是
    LineStyle
  • 因为您正在将任何对角线边界设置为“无”,所以不需要迭代范围,只需一步即可完成
  • 那么,替换

    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.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
    

    谢谢,现在立即完成。
    For Each Cell In sh2.Range("B1:F756")
        If Cell.Interior.Color = Excel.XlRgbColor.rgbWhite Then
            Cell.ClearContents
        End If
    Next