插入新行时使用带条件格式的 VLookup
Using VLookup with Conditional Formatting when Inserting a New Row
我有两张纸,每当在 Sheet 1 的第 2 行中插入一行时,我都试图将条件格式重新应用于 Sheet 1(测试 1)(我已经每当更改单元格 A2 时,我的代码都会应用条件格式)。
对于条件格式,我想使用 Vlookup 检查 A 列 Sheet 1 中的每个可见单元格是否存在于 A 列 Sheet 2 中,如果存在,则应用绿色条件格式化它。
我尝试在 Sheet 1 ("Test 1") 中使用两个不同的代码,它们都将条件格式应用于公式,但是 none当满足我用公式设置的条件时,Sheet 1 列 A 中的单元格变为绿色。
这是我的两个代码,我只需要一个就可以工作,只是公式不同:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A" 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" 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
如果我得到答案,我会更新这两个帖子,谢谢!
第二个代码段已结束,您在 Not
:
之前缺少 =
Formula1:="=Not(ISERROR(Vlookup(A2,'Test 2'!$A:$B,1,False)))"
注意下面屏幕截图中整个公式周围的引号,这是您当前拥有的:
更简单的公式如下:
Formula1:="=COUNTIF('Test 2'!$A:$A,A2)>0"
编辑:根据评论,添加第二条规则可能如下所示:
.FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=COUNTIF('Test 2'!$A:$A,A2)>0"
.FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=COUNTIF('Test 2'!$A:$A,A2)=0"
.FormatConditions(1).Interior.Color = vbGreen
.FormatConditions(2).Interior.Color = RGB(255, 199, 206)
我有两张纸,每当在 Sheet 1 的第 2 行中插入一行时,我都试图将条件格式重新应用于 Sheet 1(测试 1)(我已经每当更改单元格 A2 时,我的代码都会应用条件格式)。
对于条件格式,我想使用 Vlookup 检查 A 列 Sheet 1 中的每个可见单元格是否存在于 A 列 Sheet 2 中,如果存在,则应用绿色条件格式化它。
我尝试在 Sheet 1 ("Test 1") 中使用两个不同的代码,它们都将条件格式应用于公式,但是 none当满足我用公式设置的条件时,Sheet 1 列 A 中的单元格变为绿色。
这是我的两个代码,我只需要一个就可以工作,只是公式不同:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A" 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" 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
如果我得到答案,我会更新这两个帖子,谢谢!
第二个代码段已结束,您在 Not
:
=
Formula1:="=Not(ISERROR(Vlookup(A2,'Test 2'!$A:$B,1,False)))"
注意下面屏幕截图中整个公式周围的引号,这是您当前拥有的:
更简单的公式如下:
Formula1:="=COUNTIF('Test 2'!$A:$A,A2)>0"
编辑:根据评论,添加第二条规则可能如下所示:
.FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=COUNTIF('Test 2'!$A:$A,A2)>0"
.FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=COUNTIF('Test 2'!$A:$A,A2)=0"
.FormatConditions(1).Interior.Color = vbGreen
.FormatConditions(2).Interior.Color = RGB(255, 199, 206)