在插入新行时将VLookup与条件格式一起使用

问题描述 投票:0回答:1

我有两张工作表,每当将一行插入到工作表1的第2行中时,我都试图将条件格式重新应用于工作表1(测试1)(我使我的代码在单元格A2发生更改时就应用了条件格式。 )。

对于条件格式,我想检查列A工作表1中的每个可见单元格是否在具有Vlookup的列A工作表2中,如果确实存在,则对其应用绿色条件格式。

我有两个不同的代码要在工作表1(“测试1”)中使用,并且它们都使用条件格式和公式,但是工作表1的A列中的所有单元格都没有变为绿色。条件已设置为公式。

这里是我的两个代码,我只需要一个代码,只是不同的公式:

Private Sub Worksheet_Change(ByVal Target As Range)

 If Target.Address = "$A$2" Then

    Dim lr As Long

    lr = Range("A" & Sheet4.rows.Count).End(xlUp).Row

        With Range("A2:A" & lr)

            .FormatConditions.Delete

            .FormatConditions.Add Type:=xlExpression, Formula1:="IF(ISLBANK(Vlookup(A2,'Test 2'!$A:$B,1,False)),TRUE,FALSE)"

            .FormatConditions(1).Interior.Color = vbGreen

        End With

    End If

End Sub

我也尝试过的第二个公式是:

Private Sub Worksheet_Change(ByVal Target As Range)

 If Target.Address = "$A$2" Then

    Dim lr As Long

    lr = Range("A" & Sheet4.rows.Count).End(xlUp).Row

        With Range("A2:A" & lr)

            .FormatConditions.Delete

            .FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="Not(ISERROR(Vlookup(A2,'Test 2'!$A:$B,1,False)))"

            .FormatConditions(1).Interior.Color = vbGreen

        End With

    End If

End Sub

https://www.mrexcel.com/board/threads/using-vlookup-with-conditional-formatting-when-inserting-a-new-row.1126560/交叉发布

[如果有答案,我将同时更新两篇文章,谢谢!

excel vba vlookup conditional-formatting
1个回答
1
投票

第二个代码段关闭,您在=之前缺少一个Not

Formula1:="=Not(ISERROR(Vlookup(A2,'Test 2'!$A:$B,1,False)))"

请注意以下屏幕快照中整个公式的引号,这是您当前拥有的:enter image description here

一个更简单的公式可以是:

Formula1:="=COUNTIF('Test 2'!$A:$A,A2)>0"
© www.soinside.com 2019 - 2024. All rights reserved.