【问题标题】:Compare two Columns and format matching cells with different colors比较两列并格式化具有不同颜色的匹配单元格
【发布时间】:2016-05-17 00:19:45
【问题描述】:

感谢您在以下方面的帮助:

我想比较两列,比如说 A 列和 B 列,**寻找重复项**。

 

如果 A 列中的值与 B 列中的匹配值,我想用颜色格式化相同重复值的单元格(颜色是随机的,每次匹配的颜色不同)。

 

这是如果`A12 = B30`,颜色将为红色。如果 `A20 = B1`,则颜色为绿色,以此类推。

 

如果没有匹配,则保持原样。

 

这只是红色和绿色的一个例子。假设您有两列(A 和 B)。

A1 = 1000

A2 = 2000

A3 = 3000

A4 = 4000

A5 = 5000

A6 = 6000

A7 = 7000

A8 = 8000

A9 = 9000

 

B1 = 1500

B2 = 9000

B3 = 5000

B4 = 3500

B5 = 7500

B6 = 1000

B7 = 4000

 

所以你有几场比赛,我需要每场比赛都是随机的不同颜色。例如:

A1 = B6  –> 它们将以绿色着色/突出显示

A4 = B7   –> 它们将以红色着色/突出显示

A5 = B3 –> 它们将以黄色着色/突出显示

A9 = B2   –> 它们将以粉红色着色/突出显示

 

任何匹配的颜色都会不同,不匹配的颜色会减少或没有变化。

 

我希望这能解释问题,这必须使用 excel。

{

Sub UsingCollection()
Dim cUnique As Collection
Dim Rng As Range
Dim Cell As Range
Dim sh As Worksheet
Dim vNum As Variant for at
Dim LstRw As Long
Dim c As Range, clr As Long, x

Set sh = ActiveSheet
With sh

    LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set Rng = .Range("A1:B" & LstRw)
    Set cUnique = New Collection
    Rng.Interior.ColorIndex = xlNone
    clr = 3

    On Error Resume Next
    For Each Cell In Rng.Cells
        cUnique.Add Cell.Value, CStr(Cell.Value)
    Next Cell
    On Error GoTo 0

    For Each vNum In cUnique

        For Each c In Rng.Cells
            If c = vNum Then
                x = Application.WorksheetFunction.CountIf(Rng, vNum)
                If x > 1 Then c.Interior.ColorIndex = clr "error here: the code runs fine for around 50 lines then it is stoppedand gives error and pointing to this line"
                  //Error shows in pop window: Run-time error 'g': Subscript out of range
            End If
        Next c
        clr = clr + 1
     Next vNum

   End With


End Sub

}

【问题讨论】:

  • 样本数据和预期输出请您说过,一旦您想以红色突出显示,而其他时候以绿色突出显示,有点混乱。
  • 那只是红色和绿色的例子..
  • 我修改了测试,希望通俗易懂
  • 使用 excel 是不是指 VBA,这很难做到,因为即使使用条件格式,也仅限于可以有多少个条件。
  • 将示例工作簿上传到您的 GoogleDrive,然后分享链接

标签: excel excel-2010 excel-2007


【解决方案1】:

这是根据我的回答调整后的代码。

https://stackoverflow.com/a/33798531/1392235

遍历单元格以查找唯一值,然后遍历唯一值以对重复项着色。

Sub UsingCollection()
    Dim cUnique As Collection
    Dim Rng As Range
    Dim Cell As Range
    Dim sh As Worksheet
    Dim vNum As Variant
    Dim LstRw As Long
    Dim c As Range, clr As Long, x

    Set sh = ActiveSheet
    With sh

        LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set Rng = .Range("A1:B" & LstRw)
        Set cUnique = New Collection
        Rng.Interior.ColorIndex = xlNone
        clr = 3

        On Error Resume Next
        For Each Cell In Rng.Cells
            cUnique.Add Cell.Value, CStr(Cell.Value)
        Next Cell
        On Error GoTo 0

        For Each vNum In cUnique

            For Each c In Rng.Cells
                If c = vNum Then
                    x = Application.WorksheetFunction.CountIf(Rng, vNum)
                    If x > 1 Then c.Interior.ColorIndex = clr
                End If
            Next c
            clr = clr + 1
        Next vNum

    End With

End Sub

结果

Sample Workbook

编辑:

使用 colorindex 将我们限制为 56 种颜色,如果我们使用 RGB,我们可以增加它。编辑这部分代码,您将不得不使用这些值来获得您喜欢的颜色变化。

       If x > 1 Then c.Interior.Color = 1000000 + clr * 100
            End If
        Next c
        clr = clr + 255

【讨论】:

  • 谢谢你的回复,但是这个可以用在excel里吗
  • 是的,请参阅示例工作簿,它是一个启用宏的工作簿,您必须在打开它时启用宏
  • 感谢 Davesexcel。但是,我运行宏并得到错误:运行时错误'g':下标超出范围。我的列是 D 和 E。我认为问题在于范围。我在 LstRw = .Cells(.Rows.Count, "D").End(xlUp).Row Set Rng = .Range("D1:E" & LstRw) 列上修改了你的代码 D 值几乎没有变化,如果 D 有大约 100 行。 Cloun E 的内容正在发生变化(这是我想与 D 的值进行比较的内容)并且行数有所不同。请你指教
  • 在这一行 If x > 1 Then c.Interior.ColorIndex = clr
  • 你能把完整的代码放在你原来的问题里吗,在你原来的问题下面,点击编辑,然后添加你的代码使用,选择代码并使用{}图标包装代码。并解释发生了什么。 - 在 cmets 框中放置多行代码,难以阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
相关资源
最近更新 更多