如何在指定的列中更改单元格颜色?

发布于 2025-01-18 03:46:27 字数 524 浏览 3 评论 0原文

当Ativecell在6行后面时,我选择了此代码以选择和更改整体的内部颜色(绿色)。

我需要选择并更改该行的“ I”和“ J”列的内部颜色(颜色= 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

I picked up this code to select and change the interior color (green) of the EntireRow when the AtiveCell is behind the 6 Row.

I need to select and change the interior color (Color = 9359529) of the column "I" and "J" of the Row where is the ActiveCell. Is similar to this code but do not need the entire row, just the columns I and 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反话 2025-01-25 03:46:27

仅使用您的榜样,我认为这是做我想过的最简单方法。

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

以使用范围对象 - 但这很容易理解

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

Using just your example and what I think you're asking this is the simplest way to do what I think you're asking.

You either have just one row in the selection - or you just want the first row changed

This can be changed to use a Range object - but this is easy to understand

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
轮廓§ 2025-01-25 03:46:27

工作表 SelectionChange

  • 非常感谢 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

A Worksheet SelectionChange

  • Many thanks to Tragamor for pointing out the many flaws of my previous attempts.
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文