【问题标题】:Colorize cells containing data in a selected range为包含选定范围内数据的单元格着色
【发布时间】:2017-01-25 18:16:57
【问题描述】:

有人知道如何实现以下内容吗?

我在 Excel 工作表中选择了一系列单元格。按下快捷键后,所选范围内包含数据的每个单元格都应填充为红色。

似乎没有默认的 Excel 功能,但也许可以使用 VBA 完成?

很遗憾,以下代码无法按预期工作:

Sub ColorizeCells()
    Dim Data As Range
    Dim cell As Range
    Set Data = Selection

    For Each cell In Data
        If Not cell.Value Is Nothing Then
            cell.Interior.ColorIndex = 4
        End If
    Next

End Sub

对此的任何建议都非常感谢。

【问题讨论】:

  • 条件格式呢?例如,公式可以是A2<>""?你试过什么?
  • internet 上有大量可用选项。你试过吗?
  • @ManishChristian 他们中的大多数确实使用我尝试过但并不真正喜欢的条件格式。其余的不考虑只考虑预定义中的单元格。
  • 分享一些你迄今为止尝试过的虚拟数据和代码。
  • If Len(cell.Value) > 0 Then

标签: excel vba


【解决方案1】:

蒂姆·威廉姆斯的暗示成功了。谢谢!

以下代码有效:

Sub ColorizeCells()
    Dim Data As Range
    Dim cell As Range
    Set Data = Selection

    For Each cell In Data
        If Len(cell.Value) > 0 Then
            cell.Interior.ColorIndex = 4
        End If
    Next
End Sub

【讨论】:

    猜你喜欢
    • 2015-10-11
    • 2019-07-02
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多