如何避免使用themecolor运行时错误

时间:2013-05-03 16:53:12

标签: excel excel-vba vba

在宏的某个时刻,我想删除我在单元格中使用绿色文本完成的所有注释。

为此,我只使用2“for”循环:

For Each WS In WB.Worksheets
For Each cell In WS.UsedRange.Cells

    If cell.Font.ThemeColor = xlThemeColorAccent3 Then
        cell.Value = ""

    End If
Next
Next

但有时候主题颜色似乎没有定义,代码因运行时错误而停止'5'无效的过程调用或参数。

我试图自己找到解决方案。由于我有许多其他颜色,我无法为每个单元格设置默认值。

如何避免此运行时错误?

1 个答案:

答案 0 :(得分:1)

如果只是为了避免它,你可以创建一些不那么好的try-catch(在VBA中不存在),如下所示:

    For Each WS In WB.Worksheets
        For Each cell In WS.UsedRange.Cells

            on error goto 1
            If cell.Font.ThemeColor = xlThemeColorAccent3 Then
                cell.Value = ""

            End If
1           on error goto 0

       Next
    Next