通过细胞迭代改变细胞颜色太慢了

时间:2018-05-06 10:23:24

标签: excel vba excel-vba

如果工作簿中的所有单元格都没有颜色,我想将其更改为白色内部。我有一个工作宏(见下文),然而,它非常慢并经常崩溃。如果我手动单步执行代码或者不会崩溃,它可以很好地工作,但当然非常慢。

任何人都可以帮我找到如何让这个表现更快吗?

亲切的问候, 弗兰克

Sub Fill_Cells_White_When_Blank()
    Dim mSheet As Worksheet
    Dim mCell As Range
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    For Each mSheet In Workbooks("Name.xlsx").Worksheets
        For Each mCell In mSheet.UsedRange
            On Error Resume Next
            If mCell.Interior.ColorIndex = xlNone Then
                mCell.Interior.ColorIndex = 2
            End If

        Next
    Next

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

2 个答案:

答案 0 :(得分:1)

您可以尝试Union范围,然后像下面一样更改ColorIndex,看看它是否运行得更快。

Sub Fill_Cells_White_When_Blank()
    Dim mSheet As Worksheet
    Dim mCell As Range, mCells As Range
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    On Error Resume Next    
    For Each mSheet In Workbooks("Name.xlsx").Worksheets
        Set mCells = Nothing
        For Each mCell In mSheet.UsedRange
            If mCell.Interior.ColorIndex = xlNone Then
                If mCells Is Nothing Then
                    Set mCells = mCell
                Else
                    Set mCells = Union(mCells, mCell)
                End If
            End If
        Next
        If Not mCells Is Nothing Then mCells.Interior.ColorIndex = 2
    Next

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

答案 1 :(得分:0)

您可以使用“查找和替换”对话框。它允许您选择要搜索的格式并替换为您需要的格式。尝试一下。

SO_Find_and_Replace