如何用vba代码?当选择excel某行,那一行的A列单元格值为1时整行填充绿

2025-02-07 20:40:16
推荐回答(3个)
回答1:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) '选区改变时
    If Selection.Rows.Count = 1 And Selection.Columns.Count = 256 And Cells(ActiveCell.Row, 1) = 1 Then '选中整行,并且该行第一列值为 1
        Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 256)).Interior.Color = RGB(0, 255, 0) '整行单元格颜色为绿色(RGB 0,255,0)
    End If
End Sub

回答2:

Sub 按钮1_Click()
Row = ActiveCell.Row
If Cells(Row, 1) = 1 Then
    Rows(ActiveCell.Row).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End If
End Sub


回答3:

给sheet添加SelectionChange事件代码
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Rows.Count = 1 And Target.Columns.Count = 256 Then
If ThisWorkbook.ActiveSheet.Range("A" & Target.Row) = 1 Then
ThisWorkbook.ActiveSheet.Range(Target.Row & ":" & Target.Row).Interior.ColorIndex = 10
End If
End If
End Sub