如何制作 VBA 代码来改变单元格颜色?
How to make VBA code to change cells colors?
我拿起这段代码 select 并在 ativecell 位于第 6 行后面时更改 EntireRow 的内部颜色(绿色)。我需要将此代码更改为 select 并更改活动单元所在行的“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
仅使用您的示例和我认为您要问的内容,这是完成我认为您要问的事情的最简单方法。
您在选择中只有一行 - 或者您只想更改第一行
这可以更改为使用 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
工作表选择更改
- 非常感谢 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
我拿起这段代码 select 并在 ativecell 位于第 6 行后面时更改 EntireRow 的内部颜色(绿色)。我需要将此代码更改为 select 并更改活动单元所在行的“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
仅使用您的示例和我认为您要问的内容,这是完成我认为您要问的事情的最简单方法。
您在选择中只有一行 - 或者您只想更改第一行
这可以更改为使用 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
工作表选择更改
- 非常感谢 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