【问题标题】:How to change cells colors in specified columns?如何更改指定列中的单元格颜色?
【发布时间】:2022-07-14 14:19:06
【问题描述】:

当 AtiveCell 在 6 Row 后面时,我选择并更改 EntireRow 的内部颜色(绿色)。

我需要选择并更改 ActiveCell 所在行的“I”和“J”列的内部颜色 (Color = 9359529)。与此代码类似,但不需要整行,只需要 I 和 J 列。

Dim lTarget As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   
    If Target.Row >= 6 Then
       
        If Not lTarget Is Nothing Then
            lTarget.EntireRow.Interior.ColorIndex = 0
        End If
        
        Target.EntireRow.Interior.Color = 9359529
        Set lTarget = Target

    End If

End Sub

【问题讨论】:

    标签: excel vba colors cell


    【解决方案1】:

    仅使用您的示例以及我认为您要问的问题,这是完成我认为您要问的最简单的方法。

    您要么在选择中只有一行 - 要么您只想更改第一行

    这可以更改为使用 Range 对象 - 但这很容易理解

    Dim lTarget As Range
    Const TargetCol1    As Integer = 9
    Const TargetCol2    As Integer = 10
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
       
        If Target.Row >= 6 Then
            If Not lTarget Is Nothing Then
                lTarget.EntireRow.Interior.ColorIndex = 0
            End If
            
            Cells(Target.Row, TargetCol1).Interior.Color = 9359529
            Cells(Target.Row, TargetCol2).Interior.Color = 9359529
            
            Set lTarget = Target
        End If
    End Sub
    

    【讨论】:

    • 非常感谢 dbmicth,这正是我完成工作所需要的。
    • 太棒了 - 如果它对你有用,你能完成并检查它作为答案吗?非常感谢。
    • 当然是 dbmitch。我试图了解我在哪里可以做到这一点,因为我是 stackoverflow 的新手。我希望我在正确的地方做到了。非常感谢您的宝贵回答。
    • dbmitch 先生,它在“Cells(Target.Row, TargetCol1).Interior.Color = 9359529”行给出错误的代码。也许是因为我有一个带有公式的条件格式,如果 =$I16="" 然后将表格的所有行保持为灰色。否则,该行将保留 color=0 以及我为某些列指定的所有其他条件格式颜色。也许是这段代码的原因,它在“Cells(Target.Row, TargetCol1).Interior.Color = 9359529”上给出了错误。当我在没有保护工作表的情况下工作时,它工作得很好,但是当输入密码来保护页面时,它开始出现错误
    • 只要页面没有受到保护,代码就可以完美运行。保护她后,启动报错。
    【解决方案2】:

    工作表选择更改

    • 非常感谢 Tragamor 指出我之前尝试的许多缺陷。
    Option Explicit
    
    Private lTarget As Range
    Private FirstPassed As Boolean
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        
        Const FirstRow As Long = 6
        Const Cols As String = "I:J"
        Const iColor As Long = 9359529
        
        Dim rrg As Range
        Set rrg = Rows(FirstRow).Resize(Rows.Count - FirstRow + 1)
        Dim irg As Range: Set irg = Intersect(rrg, Target)
        If Not irg Is Nothing Then Set irg = Intersect(irg.EntireRow, Columns(Cols))
        
        If FirstPassed Then
            If irg Is Nothing Then
                If Not lTarget Is Nothing Then
                    lTarget.Interior.ColorIndex = xlNone
                    Set lTarget = Nothing
                End If
            Else
                If Not lTarget Is Nothing Then
                    lTarget.Interior.ColorIndex = xlNone
                End If
                irg.Interior.Color = iColor
                Set lTarget = irg
            End If
        Else
            rrg.Columns(Cols).Interior.ColorIndex = xlNone
            If Not irg Is Nothing Then
                irg.Interior.Color = iColor
                Set lTarget = irg
            End If
            FirstPassed = True
        End If
    
    End Sub
    

    【讨论】:

    • 这将留下触发更改的前一个活动单元格的突出显示。您可能希望使用全局/工作表变量进行更新以保存该值并在更改时清除。
    • 谢谢VBasic2008。作为 dbmitch 的代码,两者都完全符合我的需要。非常感谢两位为完成我的工作所提供的宝贵帮助..
    • VBasic2008,它在“rrg.Columns(Cols).Interior.ColorIndex = xlNone”行给出错误的代码。也许是因为我有一个带有公式的条件格式,如果 =$I16="" 然后将表格的所有行保持为灰色。否则,该行将保留 color=0 以及我为某些列指定的所有其他条件格式颜色。也许是这段代码在“rrg.Columns(Cols).Interior.ColorIndex = xlNone”上给出错误的原因。当我使用没有保护工作表的代码时,它工作得很好,但是当我输入密码来保护页面时,它开始出现错误。
    • 当我保护页面时,首先,在 (lTarget.Interior.ColorIndex = xlNone) 上给出错误 - 代码:“Else If Not lTarget Is Nothing Then lTarget.Interior.ColorIndex = xlNone End If”和之后,当我结束错误时,转到另一个错误( rrg.Columns(Cols).Interior.ColorIndex = xlNone) - 代码:“Else rrg.Columns(Cols).Interior.ColorIndex = xlNone If Not irg Is Nothing那么”
    • 只要页面没有受到保护,代码就可以完美运行。保护她后,启动报错。
    猜你喜欢
    • 2023-02-07
    • 2014-08-24
    • 2011-06-23
    • 2015-01-28
    • 2013-10-30
    • 2013-01-21
    • 1970-01-01
    • 2018-07-20
    • 2020-04-28
    相关资源
    最近更新 更多