Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何仅使用VBA高亮显示活动行_Excel_Vba_Autofilter - Fatal编程技术网

Excel 如何仅使用VBA高亮显示活动行

Excel 如何仅使用VBA高亮显示活动行,excel,vba,autofilter,Excel,Vba,Autofilter,我有一个代码将两个条件格式规则添加到第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

我有一个代码将两个条件格式规则添加到第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)。我在上面代码中的
结束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类型:=xlExpression,运算符:=xlExpression,公式1:=“=单元格(“行”)=row()”

(二) 出于另一个目的,我在“MyRange”中添加了一行来定义活动行中a列的名称,因此我现在也尝试添加此代码,而不是条件格式:

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
第二种解决方案的错误在于
范围(“B:E”和(ActiveCell.Row)).Interior.Color=RGB(243243123)

代码的输出应该如下所示


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


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

    Cells.Interior.ColorIndex = 0

    With Target

        'Highlights the entire row that contain the active cell
        .EntireRow.Interior.Color = RGB(243, 243, 123)

    End With

End Sub

要仅高亮显示活动行,请执行以下操作:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Cells.Interior.ColorIndex = 0

    With Target

        'Highlights the entire row that contain the active cell
        .EntireRow.Interior.Color = RGB(243, 243, 123)

    End With

End Sub

以下是完成此类任务的一般方法:

  • 创建工作簿名称“THE_ROW”,并将其初始值设置为0

  • 将基于公式的条件格式添加到感兴趣的范围:

  • 3.对于选择更改事件:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        ThisWorkbook.Names("THE_ROW").RefersTo = "=" & Target(1).Row
    End Sub
    
    不完全符合规定,因为它只突出显示填充的单元格


    在设置填充时使用CF意味着您无需担心在突出显示行时覆盖现有填充。

    我会在此处使用
    选择更改
    路线,但可能有一种比您当前尝试的更简单的方法。是的,我同意,
    选择更改
    似乎是一种更好的路线,正如我所见我已经把它用到了
    范围(“A”&(ActiveCell.Row))。选择
    作为另一个目的,我想把它添加到这里。你知道为什么它可能不起作用吗?
    “B:E”&(ActiveCell.Row)
    没有为您提供有效的范围引用。您需要为每列重复行号,因此输出类似于
    B5:E5
    。我同意,我找到了一个有效的解决方案,请参见下文