如果在范围内找到Cell而不是更改单元格颜色,则留下白色

时间:2018-03-08 03:24:20

标签: excel vba excel-vba excel-formula

我曾尝试在多个网站上进行多次搜索,但我无法找到实际适用于我的工作的答案

我有工作表A,B和C

Sheet An用作存储数据的列表 表B指的是这些数据 表C指的是此数据

如果工作表A中的单元格A2与工作表B或C的范围完全匹配,则颜色如果不是,则保留默认白色

以下是我根据收到的答案尝试的代码。

我使用了建议的“A”列并尝试使该语句为true(路由2的A列中的单元格更改为匹配属性列表中的单元格A2),我无法将单元格更改为绿色

Sub main()
    Dim shtA As Worksheet, shtB As Worksheet, shtC As Worksheet, shtD As Worksheet
    Set shtA = Worksheets("Property List") 'change "A" to your actual Sheet A name
    Set shtB = Worksheets("Route 2") 'change "B" to your actual Sheet A name
    Set shtC = Worksheets("Route 3") 'change "C" to your actual Sheet A name
    Set shtD = Worksheets("Route 4E") 'change "C" to your actual Sheet A name

    With shtA.Range("A2")
        .Interior.Color = IIf(Not IsThere(shtB, .Value) And Not IsThere(shtC, .Value), vbGreen, vbWhite) 'change 'vbGreen' to your actual color to mark the match
    End With
End Sub

Function IsThere(sht As Worksheet, val As Variant) As Boolean
    With sht
         IsThere = Not .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Find(what:=val, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
    End With
End Function

1 个答案:

答案 0 :(得分:0)

你可以如下:

Sub main()
    Dim shtA As Worksheet, shtB As Worksheet, shtC As Worksheet
    Set shtA = Worksheets("A") 'change "A" to your actual Sheet A name
    Set shtB = Worksheets("B") 'change "B" to your actual Sheet A name
    Set shtC = Worksheets("C") 'change "C" to your actual Sheet A name

    With shtA.Range("A2")
        .Interior.Color = IIf(Not IsThere(shtB, .value) And Not IsThere(shtC, .value), vbGreen, vbWhite) 'change 'vbGreen' to your actual color to mark the match
    End With
End Sub

Function IsThere(sht As Worksheet, val As Variant) As Boolean
    With sht
         IsThere = Not .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Find(what:=val, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
    End With
End Function

在函数IsThere中,我假设要在列#34; A"中搜索匹配项。传递的工作表。如果没有,请更改" A2"和" A"到你的实际列参考