从Excel范围中识别同一行中的相似值

时间:2018-11-27 11:22:25

标签: excel vba excel-vba

我有一个Excel工作表数据列B2:AF7 像这样的数据...

B         C      D      E       F        G
1919106 1888033 1885508 1882576 1881697 1882508
1825168 1835058 1822453 1995037 1812891 1806778
1856294 1847202 1852195 2008647 1850637 1846161
2038838 1888345 1884439 1877693 1871343 1866290
1891278 1887332 1881356 1871914 1865842 2003296
1799495 1793926 1944344 1786693 1783027 1778177

例如:
如果用户在C12,H12和M12行中输入以下三个值 1851060, 1829639 and 1983373, 然后用相同的颜色突出显示各列中的三种不同颜色。(请参考图像,这些颜色用颜色突出显示),遵循以下代码即可正常工作。

但是我需要在同一行中突出显示相似的值。

我将IF AND与基于颜色的条件一起使用,但是执行效果很好。

Public Function fall()                                       
    Dim ra As Range
    Dim ra1 As Range
    Dim ra2 As Range
    Dim i As Integer

    Range("b2:af7").Interior.ColorIndex = 0

    For i = 12 To 19
        Set ra = Cells.Find(What:=Range("c"&i), LookIn:=xlFormulas, lookat _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)

        Set ra1 = Cells.Find(What:=Range("h"&i), LookIn:=xlFormulas, lookat _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)

        Set ra2 = Cells.Find(What:=Range("m&"i), LookIn:=xlFormulas, lookat _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)


        If ra Is Nothing Then
            MsgBox ("Not found")
        Else
            ra.Interior.ColorIndex = 3         
            ra1.Interior.ColorIndex = 4       
            ra2.Interior.ColorIndex = 6      
        End If

        ' for same row with color based criteria
        If ra.Interior.ColorIndex = 3 And ra1.Interior.ColorIndex = 4 And ra2.Interior.ColorIndex = 6 Then
            rng1.Interior.ColorIndex = 15
            rng2.Interior.ColorIndex = 15
            rng3.Interior.ColorIndex = 15
        Else
            MsgBox "not found"
            Exit Function
        End If             
    Next i
End Function

Picture of table

0 个答案:

没有答案