如何仅使用VBA突出显示活动行

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

我有一个代码将两个条件格式设置规则添加到工作表4(A2列:A和最后一行),现在我要突出显示活动行,直到最后一列(例如,活动列的B:E列行)。目前,我有此代码:

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:="=COUNTIF('Test 2'!$A:$A,A2)>0"

            .FormatConditions(1).Interior.Color = RGB(198, 239, 206)

            .FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=COUNTIF('Test 2'!$A:$A,A2)=0"

            .FormatConditions(2).Interior.Color = RGB(255, 199, 206)

        End With 

    End If

End Sub

我有两个可能的解决方案]

((1)在A列之后向活动行添加条件格式(例如,活动行的B:E。我在上面代码的End If之后使用此代码。

If Application.CutCopyMode = False Then
Application.Calculate
End If

With Target

.FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=CELL("row")=ROW()"
.FormatConditions(1).Interior.Color = RGB(220, 239, 206)

End With

我似乎对于条件格式.FormatConditions.Add Type:=xlExpression, Operator:=xlExpression, Formula1:="=CELL("row")=ROW()"的公式出错了>

(2)我添加了一行以将“活动行”中的列A的名称定义为“ MyRange”,以用于其他目的,因此我现在也尝试添加此代码,而不是条件格式:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    ActiveWorkbook.Names.Add Name:="MyRange", RefersToR1C1:=Range("A" & (ActiveCell.Row))
    Range("A" & (ActiveCell.Row)).Select                                                   'Always Selects Column A depending on the Active Row selected
    Range("B:E" & (ActiveCell.Row)).Interior.Color = RGB(243, 243, 123)

End Sub

第二种解决方法有Range("B:E" & (ActiveCell.Row)).Interior.Color = RGB(243, 243, 123)的错误

代码的输出应如下所示enter image description here

enter image description here

enter image description here

我有一个代码将两个条件格式设置规则添加到工作表4(列A2:A和最后一行),现在我要突出显示活动行,直到最后一列(例如,的列B:E。 ..

excel vba autofilter
1个回答
0
投票

解决方案2,突出显示活动行:

© www.soinside.com 2019 - 2024. All rights reserved.