【问题标题】:Highlight Current Row Without Deleting Existing Cell Colours突出显示当前行而不删除现有单元格颜色
【发布时间】:2017-05-08 17:37:13
【问题描述】:

我找到了下面的代码,虽然它突出显示了整行,但它还从任何先前着色的单元格中删除了颜色。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Target.Parent.Cells.Interior.ColorIndex = 0
    With Target
        ' Highlight the entire row and column that contain the active cell
        .EntireRow.Interior.ColorIndex = 8
    End With
    Application.ScreenUpdating = True

End Sub

我想在选择单元格时突出显示整行(可能已经着色),但是当我移动到不同行中的单元格时,之前突出显示的行应该返回到之前的颜色。

有没有办法修改之前选择的单元格/行?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    条件格式会覆盖“常规”格式(不替换它),因此,如果您还没有应用一些 CF,这是一种方便的方法来突出显示一行,而不会改变任何现有的单元格颜色。

    这是一个非常基本的例子:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Cells.Count > 1 Then Exit Sub
    
        Application.ScreenUpdating = False
    
        Me.Cells.FormatConditions.Delete
    
        With Target.EntireRow.FormatConditions.Add(Type:=xlExpression, _
                                                   Formula1:="=TRUE") 
            .SetFirstPriority
            .Interior.Color = 65535
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    

    【讨论】:

    • 如何将行颜色黄色更改为另一种颜色,例如天空或灰色
    • 用您想要的颜色为单元格着色,选择它,然后转到 VB 编辑器中的“立即”面板并键入 ? Selection.Interior.Color 并按 Enter。用你得到的号码交换 65535。
    • 我找到了另一种更新行颜色示例的方法 - .Interior.Color = RGB(121, 121, 121)。谢谢!
    【解决方案2】:

    您需要将格式和行号存储在某处,然后在选择新行时将其粘贴回来。

    这会将突出显示之前的现有格式和行号存储到同一工作表上的 1,040,000 行。

    然后选择另一行时,它将检查是否有格式化并替换从复制后面的行。

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    If Target.Cells.Count > 1 Then Exit Sub
    
        Application.ScreenUpdating = False
        'test if formatting exist and copy it back to the row just left.
        If Cells(1040000, 1) <> "" Then
            Rows(1040000).Copy
            Rows(Cells(1040000, 1).Value).PasteSpecial Paste:=xlPasteFormats
        End If
        'Copy formating to store
        Rows(Target.Row).Copy
        Rows(1040000).PasteSpecial Paste:=xlPasteFormats
        Cells(1040000, 1) = Target.Row
    
    
        With Target
            ' Highlight the entire row and column that contain the active cell
            .EntireRow.Interior.ColorIndex = 8
    
        End With
    
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      我为此创建了一个add-in。下载,启用内容,然后单击安装按钮。该加载项在“查看”功能区选项卡上创建了三个用于切换突出显示的按钮。

      • 使用条件格式,因此没有覆盖单元格颜色设置。
      • 所有代码都在加载项中,因此不需要额外的 VBA。

      【讨论】:

      • 加载项代码在我点击安装命令之前看起来是合法的,并且它像宣传的那样工作。我认为,如果您有多个电子表格(并不总是您自己的),并且您希望在其中快速启用此功能,那么这是更好的解决方案。
      【解决方案4】:

      这是我能想到的:

      Public rngPreviousColor As Range
      Public lngColor         As Long
      
      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      
          If Target.Cells.Count > 1 Then Exit Sub
      
          If Not rngPreviousColor Is Nothing Then
              rngPreviousColor.Interior.ColorIndex = lngColor
          End If
      
          Set rngPreviousColor = Target.EntireRow
          lngColor = rngPreviousColor.Interior.ColorIndex
      
          With Target
              .EntireRow.Interior.ColorIndex = 8
          End With
      
      End Sub
      

      想法是另一行是一种颜色的整体,我们将该行保存为范围rngPreviousColor,颜色保存为lngColor

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 1970-01-01
        • 2014-02-21
        • 1970-01-01
        • 2018-06-01
        • 2018-05-30
        • 2018-03-12
        • 2016-08-20
        • 2019-04-22
        相关资源
        最近更新 更多