【问题标题】:How to fill color in a cell in VBA?如何在VBA中的单元格中填充颜色?
【发布时间】:2014-03-05 09:21:33
【问题描述】:

我想为当前工作表中具有“#N/A”值的单元格着色。为此,我使用以下宏:

Sub ColorCells()

Dim Data As Range
Dim cell As Range
Set currentsheet = ActiveWorkbook.Sheets("Comparison")
Set Data = currentsheet.Range("A2:AW1048576")

For Each cell In Data
If cell.Value = "#N/A" Then
   cell.Interior.ColorIndex = 3
End If
Next

End Sub

但是If cell.Value = "#N/A" Then 行给出了一个错误:类型不匹配。也许有人可以帮助了解错误在哪里?谢谢

【问题讨论】:

  • 为什么不直接使用条件格式来突出显示有错误的单元格?)如果您不喜欢它,请使用If cell.Text = "#N/A" Then。还有一个提示,尝试使用Set Data = Intersect(currentsheet.UsedRange,currentsheet.Range("A2:AW1048576")) 来最小化循环中的单元格数。现在你循环遍历 5000 万 个细胞:)
  • 你也可以使用IsError(Cell.Value)
  • 而不是 .value 使用 .text

标签: vba excel


【解决方案1】:

非 VBA 解决方案:

使用带有公式的条件格式规则:=ISNA(A1)(要突出显示有所有错误的单元格-不仅是#N/A,还可以使用=ISERROR(A1)

VBA 解决方案:

您的代码循环通过 5000 万 个单元格。为了减少单元格的数量,我使用.SpecialCells(xlCellTypeFormulas, 16).SpecialCells(xlCellTypeConstants, 16)只返回有错误的单元格(注意,我使用的是If cell.Text = "#N/A" Then

Sub ColorCells()
    Dim Data As Range, Data2 As Range, cell As Range
    Dim currentsheet As Worksheet

    Set currentsheet = ActiveWorkbook.Sheets("Comparison")

    With currentsheet.Range("A2:AW" & Rows.Count)
        .Interior.Color = xlNone
        On Error Resume Next
        'select only cells with errors
        Set Data = .SpecialCells(xlCellTypeFormulas, 16)
        Set Data2 = .SpecialCells(xlCellTypeConstants, 16)
        On Error GoTo 0
    End With

    If Not Data2 Is Nothing Then
        If Not Data Is Nothing Then
            Set Data = Union(Data, Data2)
        Else
            Set Data = Data2
        End If
    End If

    If Not Data Is Nothing Then
        For Each cell In Data
            If cell.Text = "#N/A" Then
               cell.Interior.ColorIndex = 4
            End If
        Next
    End If
End Sub

注意,要突出显示任何错误的单元格(不仅是"#N/A"),请替换以下代码

If Not Data Is Nothing Then
   For Each cell In Data
       If cell.Text = "#N/A" Then
          cell.Interior.ColorIndex = 3
       End If
   Next
End If

If Not Data Is Nothing Then Data.Interior.ColorIndex = 3

UPD:(如何通过VBA添加CF规则)

Sub test()
    With ActiveWorkbook.Sheets("Comparison").Range("A2:AW" & Rows.Count).FormatConditions
        .Delete
        .Add Type:=xlExpression, Formula1:="=ISNA(A1)"
        .Item(1).Interior.ColorIndex = 3
    End With
End Sub

【讨论】:

  • 谢谢!它工作正常,两种方法!如果我需要使用另一个输入运行宏并且 N/A 单元格的位置发生变化,则 VBA 的唯一问题是,此宏不会重写颜色。例如,如果 A2 单元格被着色,即使从第二次尝试不再有 N/A 值,它也会保持着色。
  • @Ale,在这种情况下,在 On Error Resume Next 之前添加行 currentsheet.Range("A2:AW" & Rows.Count).Interior.Color = xlNone(请参阅我的更新答案)
  • 可能是 A 列必须始终以灰色着色,但 B 列包含一些值和 N/A,以红色着色。包含 N/A 的单元格取决于输入文件和另一个宏。因此,如果 N/A 位置发生变化,我必须清除以前的红色,但仍将 A 列保留为灰色。但在这种情况下,使用 CF 可能更好。谢谢!
  • @Ale,请参阅我的更新部分,了解如何通过 VBA 创建 CF 规则:)
  • 谢谢,它工作正常!但后来我实际上录制了一个做 CF 的宏,所以 VBA 自动创建了命令。 :)
【解决方案2】:
  1. 使用条件格式而不是 VBA 来突出显示错误。

  2. 使用像您发布的那样的 VBA 循环需要很长时间来处理

  3. 声明If cell.Value = "#N/A" Then 永远不会起作用。如果您坚持使用 VBA 突出显示错误,请尝试使用此方法。

    子颜色单元()

    Dim Data As Range
    Dim cell As Range
    Set currentsheet = ActiveWorkbook.Sheets("Comparison")
    Set Data = currentsheet.Range("A2:AW1048576")
    
    For Each cell In Data
    
    If IsError(cell.Value) Then
       cell.Interior.ColorIndex = 3
    End If
    Next
    
    End Sub
    
  4. 为漫长的等待做好准备,因为该过程会循环通过 5100 万个单元

  5. 有更有效的方法可以实现您想做的事情。如果您改变主意,请更新您的问题。

【讨论】:

  • 谢谢!好吧,实际上我正在寻找 VBA 解决方案,但它工作正常。
【解决方案3】:
  1. 按左上角选择所有单元格
  2. 选择【首页】>>【条件格式】>>【新建规则】
  3. 选择[仅格式化包含以下内容的单元格]
  4. 在 [Format only cells with:] 中,选择 “错误”
  5. 在 [Format..] 按钮中选择适当的格式

【讨论】:

    【解决方案4】:

    您需要使用 cell.Text = "#N/A" 而不是 cell.Value = "#N/A"。单元格中的错误实际上只是单元格中存储的文本。

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 2017-01-05
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多