【问题标题】:Keeping the first row format after selecting other active cells选择其他活动单元格后保持第一行格式
【发布时间】:2020-03-13 03:53:36
【问题描述】:

当前,代码在选择排除行1的任何活动单元格时始终选择活动行的列“ A”,并将该列命名为单元格“我的范围”。这就是前几行所实现的,之后我试图突出显示第 2 行之后的任何活动行,并在选择另一个活动行时清除突出显示。

选择另一个活动行时似乎有问题,因为前一行没有清除颜色。

所以在示例中,我选择了测试 1(第 3 行是活动行):

选择另一个活动行时,例如测试2(第4行现在为活动行),活动行是颜色的,但先前彩色的行3仍在

中彩色

每当我选择另一个活动行时,前一行都不会清除它的颜色,我想在第 1 行保持任何颜色(I1 列中的黑色填充),这就是我希望输出在我选择时的样子另一个活动行,所以在这种情况下,我测试 1 是活动行(第 3 行),但是当我选择测试 2(第 4 行)作为活动行时,第 3 行清除它的颜色,如下所示:

这是我正在运行的整个代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.ScreenUpdating = False                                                          'This speeds up the macro by hiding what the macro is doing

        If Target.Row > 1 Then

        ActiveWorkbook.Names.Add Name:="MyRange", RefersToR1C1:=Range("A" & (ActiveCell.Row))   'Defines the name of the ActiveCell as "MyRange" to Autofill VLookup Formula on sheet

        Range("A" & (ActiveCell.Row)).Select                                                    'Always Selects Column A depending on the Active Row selecte

        Dim TR As Variant

        TR = Target.Row > 2

        With TR                                                                                 'With Target refers to the Active Row being selected greater than Row 2

        Target.EntireRow.Interior.ColorIndex = 0                                                'Clears Previous Cells Interior Color

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

        End With

        If Target.Address = "$A$2" Then                                                         'Checks if you have selected Row 2 (The comparison row)

            Target.Value = ""                                                                   'If Cell A2 is selected (the "Key" comparison cell from the comparison row) then a blank value is inputted

        Else

            [a2] = ActiveCell                                                                   'Makes cell "A2" equal to the Active Cell value (The "Key" in this case)

        End If

    End If

    Application.ScreenUpdating = True                                                           'Must be "True" after running the code to be able to Read/Write the Workbook

End Sub

【问题讨论】:

  • Cells.Interior.ColorIndex = 0 是工作表上的每个单元格。上一行似乎会触发您的代码进入循环。
  • 是的,我同意这一点,但我怎样才能让它避开第一行。我想也许放一个If Not 语句可以让我保持第一行的格式
  • 可能是if not(target.row=1) then ...
  • @TheGridLock 我试过这种方法,它似乎仍在清除第 1 行的内部颜色
  • 在代码的开头和结尾添加application.enableevent = false和true,比如screenupdateing,否则你不应该在代码正文中使用range.select

标签: excel vba format


【解决方案1】:

试试这个。不完全确定您在做什么,但是如果选择了向下第 3 行的单元格,这应该清除上一行。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False                                                          'This speeds up the macro by hiding what the macro is doing

If Target.Row > 1 Then                                                         'Doesn't Allow the "Titles" in Row 1 to be highlighted or changed
    ActiveWorkbook.Names.Add Name:="MyRange", RefersToR1C1:=Range("A" & (Target.Row))   'Defines the name of the ActiveCell as "MyRange" to Autofill VLookup Formula on sheet
    ActiveSheet.UsedRange.Offset(1).EntireRow.Interior.ColorIndex = 0    
    Target.EntireRow.Interior.Color = RGB(243, 243, 123)                                                                             'With Target refers to the Active Row being selected                                             'Highlights the entire row that contain the active cell

    If Target.Address = "$A$2" Then                                                         'Checks if you have selected Row 2 (The comparison row)
        Target.Value = ""                                                                   'If Cell A2 is selected (the "Key" comparison cell from the comparison row) then a blank value is inputted
    Else
        [a2] = Target                                                                  'Makes cell "A2" equal to the Active Cell value (The "Key" in this case)
    End If

    Me.Range("B2:CK2").Interior.Color = xlNone                                              'Clears any previous (if any) colouring inside cells

    Dim rng As Range                                                                        'Declares variable as a range to store values

    For Each rng In Me.Range("D2:CK2")                                                      'Declares which columns to highlight yellow if there are any parameters in Sheet 2 that vary from Sheet 1
        If IsNumeric(rng.Value) And IsNumeric(Me.Cells(Target.Row, rng.Column)) Then        '[Exludes the Key, Date, Time & Part columns: hence starting at Column D for highlighting variances]
            If rng.Value <> Me.Cells(Target.Row, rng.Column).Value Then                     'Checks if the parameters vary from the main Database ("HE 171")
                rng.Interior.Color = vbYellow                                               'Highlights any varying parameters in Yellow
            End If
        End If
    Next
End If

Application.ScreenUpdating = True                                                           'Must be "True" after running the code to be able to Read/Write the Workbook

End Sub

【讨论】:

  • 我已经编辑了我的代码,以使其更简单,以使线程更简单,代码在选择任何Active Cell(排除行1)时始终选择“活动行”的“ Active ROW”的“ A”,并重命名该列A单元格“”我的范围”暂时。这就是前几行所实现的,之后我试图突出显示第 2 行之后的任何活动行,并在选择另一个活动行时清除突出显示。 Currently when selecting another active row, the previous row is not clearing the coloring.我希望这能解决一些问题,请告诉我
  • 是的,我有,它似乎只清除了活动单元格上方的行的颜色(这是因为 ``` If Target.Row > 2 Then Target 中的offset 属性.Offset(-1).EntireRow.Interior.ColorIndex = 0 ``` 我尝试在没有offset(-1) 的情况下简单地使用您的代码,但它似乎还没有成功
  • 以上代码已修改。
  • 感谢您的回复!效果很好,只是用[a2] = ActiveCell 替换了你的[a2] = Target ,除了它可以满足我的需要
  • 好的,我只是忘了清除格式表的其余部分。 activecell 应该和 Target 一样。
猜你喜欢
  • 1970-01-01
  • 2011-06-01
  • 2018-03-26
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多