You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel如何用条件格式为非数值型重复单元格设置不同高亮颜色?

Excel 给相同值单元格设置不同高亮颜色的方法

一、指定特定值(如https://www.google.com/)高亮为红色

  • 选中需要设置的单元格区域(比如目标数据列)
  • 点击「开始」选项卡 → 「条件格式」→ 「新建规则」
  • 选择「只为包含以下内容的单元格设置格式」,在下拉菜单选「特定文本」→ 「包含」,输入https://www.google.com/
  • 点击「格式」→ 「填充」,选择红色,依次确定完成设置

二、给其他不同重复值分配不同颜色

Excel自带的「重复值」条件格式只能统一一种颜色,要给每个不同的唯一值设置专属颜色,可按以下两种方法操作:

方法1:手动条件格式设置(适合值较少的情况)

  • 选中目标区域,点击「条件格式」→ 「新建规则」
  • 选择「只为包含以下内容的单元格设置格式」→ 「等于」,输入第一个需要单独设置颜色的内容
  • 点击「格式」选择对应颜色,确定后重复此步骤,直到所有不同值都设置完成

方法2:VBA批量设置(适合值较多的情况)

  • 按Alt+F11打开VBA编辑器,点击「插入」→ 「模块」
  • 粘贴以下代码,修改代码中的目标区域(默认是A列),然后运行宏:
Sub ColorDuplicates()
    Dim rng As Range
    Dim cell As Range
    Dim uniqueValues As Collection
    Dim value As Variant
    Dim colorIndex As Integer
    
    ' 修改为你需要处理的单元格区域
    Set rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    Set uniqueValues = New Collection
    
    ' 预先添加指定值,确保其颜色为红色
    On Error Resume Next
    uniqueValues.Add "https://www.google.com/", Key:="https://www.google.com/"
    On Error GoTo 0
    
    ' 收集所有非空的唯一值
    On Error Resume Next
    For Each cell In rng
        If cell.Value <> "" Then
            uniqueValues.Add cell.Value, Key:=CStr(cell.Value)
        End If
    Next cell
    On Error GoTo 0
    
    ' 为每个唯一值分配不同颜色(跳过指定值)
    colorIndex = 4 ' 从红色(索引3)之后的颜色开始分配
    For Each value In uniqueValues
        If value <> "https://www.google.com/" Then
            rng.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=value
            rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
            With rng.FormatConditions(1).Interior
                .ColorIndex = colorIndex
            End With
            rng.FormatConditions(1).StopIfTrue = False
            colorIndex = colorIndex + 1
            ' 颜色索引超出范围时重置
            If colorIndex > 56 Then colorIndex = 4
        End If
    Next value
End Sub

内容的提问来源于stack exchange,提问作者Ankit Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:57:33