2

我有这些工作表,其中我使用绿色文本作为请求数据库的缩写,全名仅供用户使用。我认为使用不同的颜色是个好主意,这样可以轻松删除这些评论。我设法查看了工作表中所有使用过的单元格。

我现在要做的是对字体颜色进行测试。

On Error Resume Next
If cl.Font.ThemeColor = xlThemeColorAccent3 Then
    cl.Value = ""
End If
On Error GoTo 0

编辑:

我试过这个:

For Each WS In WB.Worksheets
For Each cl In WS.Range("A1:H10").Cells

    On Error GoTo Nextiteration

    If cl.Font.ThemeColor = xlThemeColorAccent3 Then
        cl.Value = ""
    End If

Nextiteration:

Next
Next

但它会删除我范围内的所有彩色单元格。

你能解释一下为什么吗?你能告诉我如何只选择绿色单元格(橄榄绿,重音 3)

4

1 回答 1

4

这是由于您放置错误处理代码的方式。当If cl.Font.ThemeColor = xlThemeColorAccent3 Then抛出异常时,它被忽略并继续执行下一步。

尝试访问其颜色当前不是主题的对象的主题颜色将导致无效的请求运行时错误。

用于Err.Number查明是否引发错误。

----Loop----

On Error Resume Next
If cl.Font.ThemeColor = xlThemeColorAccent3 Then
    If Err.Number = 0 Then ' will be 0 only if no error occurred in the previous step    
         cl.Value = ""
    End If
End If
Err.Clear ' Clear error state

----End Loop----
于 2013-05-06T10:39:53.333 回答