【问题标题】:Change the color of a text with in all the cells of an excel sheet在 Excel 工作表的所有单元格中更改文本的颜色
【发布时间】:2017-10-31 07:32:01
【问题描述】:

有没有办法更改 Excel 工作表中所有单元格中文本的颜色? 类似于查找文本并仅为 Excel 工作表的单元格更改搜索文本的前景色。

【问题讨论】:

  • 您要搜索的文本能否以不同的单词出现,不应该着色?例如。说您要搜索的单词是“猫”。 “餐饮”一词中的“猫”也应着色吗?如果是,那么您可以进行查找/替换。如果不是,那么我相信您将需要 VBA(编写代码/宏)。
  • 你能给出解决方案来改变excel表中的查找/替换文本的颜色吗?
  • @Ralph 内置功能不允许您仅更改单元格的一部分,因此 VBA 将是唯一的方法
  • @Jerry 我的测试(在下面编辑的答案中)显示整个单元格的格式被查找和替换更改,即使是部分匹配,除非我遗漏了什么?
  • 对不起,我弄错了。 VBA 的一个廉价替代方案是将范围复制到 word 并在那里进行替换,然后复制/粘贴回 excel。不,我对 VBA 不太熟悉,无法给出正确的答案(对于迟到的回复,我很抱歉,之前出现了一些我必须处理的问题)。

标签: excel


【解决方案1】:

更改excel表格所有单元格的颜色:

举个例子:

  1. 选择整个工作表或单元格区域。
  2. Home 选项卡上选择Conditional Formatting
  3. 点击New Rule...
  4. 点击Use a formula to determine which cells to format
  5. Format cells where this value is true 下输入公式: =(LEN($A$1)>0)
  6. 单击Format 并转到Fill 选项卡
  7. 选择填充颜色。点击确定,确定。

现在,如果单元格 A1 中有任何值,则在步骤 1 中选择的整个范围都会改变颜色。 您可以根据需要指定不同的单元格范围、条件或格式。 (例如,文本颜色而不是填充颜色)


编辑#1:

回复:查找和替换以更改单元格部分的颜色

查找和替换可以搜索或替换单元格格式,但替换格式会影响整个单元格。

结果:(整个单元格改变了)


编辑#2a:

您说“没有 VBA”,但为了分享可能的替代解决方案,以下是如何使用 VBA 完成此任务。该方法循环遍历ActiveSheet.UsedRange中的所有单元格:

Sub SearchReplace_Color_PartialCell()

    Const textToChange = "cat"
    Const newColor = vbRed
    Dim c As Range

    'loop throgh all cells that have data
    For Each c In ActiveSheet.UsedRange.Cells
        If InStr(c.Value, textToChange) > 0 Then 'if text exists in cell
            ' then change the color of that text
            c.Characters(InStr(c.Value, textToChange), Len(textToChange)).Font.Color = newColor 

        End If

    Next c

End Sub

当在 10,000 个单元格上运行时,每个单元格都有不同长度的字符串,中间都带有单词“cat”,此方法运行时间 2.6797 秒


编辑#2b:

另一种 VBA 解决方案,使用 .Find.FindNext 循环遍历包含数据的单元格:

Sub FindReplace_Color_PartialCell()

    Const textToChange = "cat"
    Const newColor = vbRed

    Dim c As Range, firstAddress As String

    With ActiveSheet.Cells
         Set c = .Find(textToChange, LookIn:=xlValues)
         If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Characters(InStr(c.Value, textToChange), Len(textToChange)).Font.Color = vbGreen
                Set c = .FindNext(c)
            If c Is Nothing Then
                GoTo DoneFinding
            End If
            Loop While c.Address <> firstAddress
          End If
  DoneFinding:
    End With

End Sub

当在 10,000 个单元格上运行时,每个单元格都有不同长度的字符串,中间都带有单词“cat”,此方法运行时间 8.7021 秒


编辑#2c:

修改为继续搜索一个单元格,直到找不到更多匹配项(而不是在一次替换后移动到下一个单元格):

Sub SearchReplace_Color_PartialCell()
    'modified to catch multiple occurences of search term within the single cell

    Const textToChange = "cat"
    Const newColor = vbGreen
    Dim c As Range 'the cell we're looking at
    Dim pos As Integer 'current position#, where we're looking in the cell (0 = Not Found)
    Dim matches As Integer 'count number of replacements

    For Each c In ActiveSheet.UsedRange.Cells 'loop throgh all cells that have data

        pos = 1

        Do While InStr(pos, c.Value, textToChange) > 0   'loop until no match in cell

            matches = matches + 1

            pos = InStr(pos, c.Value, textToChange)

            c.Characters(InStr(pos, c.Value, textToChange), Len(textToChange)).Font.Color = _
                newColor ' change the color of the text in that position
            pos = pos + 1 'check again, starting 1 letter to the right

        Loop

    Next c

    MsgBox "Replaced " & matches & " occurences of """ & textToChange & """"

End Sub

【讨论】:

  • 这是一个答案。但这是对另一个问题的回答(不是对上述问题的回答)。这就像有人问到机场的方向,你告诉他你不知道,然后提供到火车站的方向。好吧,让我们看看这个替代解决方案是否满足 OP 的要求。
  • 有点,是的,尽管展示了最接近的可能解决方案。即将以查找和替换不改变“部分单元格”颜色的示例来编辑答案
  • 我已经添加了 VBA 方法。它正在按我的预期工作。谢谢@ashleedawg
  • 很高兴听到!谢谢,我也学到了一些东西! (感谢@Ralph;我并没有像我喜欢挑战那样试图“证明”任何事情!)
  • @ashleedawg 两次比较不相等。在#2a 中,您只会在ActiveSheet.Cells 上使用#2b 中的Find 方法时通过ActiveSheet.UsedRange.Cells。因此,Find 似乎更慢。如果您更新#2b 以仅搜索UsedRange,那么您会看到Find 更快(自然)。这就是应该的方式,因为您不必像 #2a 中那样逐个遍历所有 cells
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 2020-01-03
相关资源
最近更新 更多