如果值会改变,改变几个单元格的颜色

If value will change, change few cells color

我找不到我的问题的答案,也许你可以帮我解决这个问题。 我想将此 VBA 脚本更改为如下内容:

如果 A3 的值会改变,那么改变 B3、C3 如果A4要改,就改B4,C4等等

A 列中的值用户将更改 "by hand"

Sub ChangeColor()

Set sht = ThisWorkbook.Worksheets("csv_vorlage")

LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

Set MyPlage = Range("A1:A" & LastRow) 'MsgBox (MyPlage) For Each cell In MyPlage Select Case cell.Value Case Is = "1" Range("B2:F2").EntireRow.Interior.ColorIndex = 3 'red Case Is = "2" cell.EntireRow.Interior.ColorIndex = 4 'green Case Is = "3" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "4" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "5" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "6" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "7" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "8" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "9" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "10" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "11" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "12" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "13" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "14" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "15" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "16" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "17" cell.EntireRow.Interior.ColorIndex = 4 Case Is = "19" cell.EntireRow.Interior.ColorIndex = 4 Case Else cell.EntireRow.Interior.ColorIndex = 0 End Select Next End Sub

以及如何做到这一点?

首先将您的代码移至 Worksheet_Change 事件,仅当 A 列中的值被修改时才检查值。

使用Select Case添加将颜色修改为绿色时要检查的多个场景。

代码

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim LastRow As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

If Not Intersect(Target, Range("A1:A" & LastRow)) Is Nothing Then
    Select Case Target.Value
        Case "1", "2", "3", "4" '<-- put the rest of your cases here
            Range("B" & Target.Row & ":C" & Target.Row & ",E" & Target.Row & ":H" & Target.Row).Interior.ColorIndex = 4  'green
        Case Else
            Range("B" & Target.Row & ":C" & Target.Row & ",E" & Target.Row & ":H" & Target.Row).Interior.ColorIndex = 0
    End Select
End If

End Sub

只要涉及到实际的着色规则,你的叙述就不清楚

但是由于你澄清的单元格将被用户更改"manually",那么你可以像下面这样:

  • 在 "csv_vorlage" 工作表代码窗格中,放置以下代码:

    Private Sub Worksheet_Change(ByVal target As Range)
        If target.Column = 1 Then ChangeColor target '<--| if any changed cell is in column A then call the color handler sub
    End Sub
    
  • 在同一代码窗格或任何其他模块中,放置以下代码

    Sub ChangeColor(target As Range)
        Dim colorIndex1 As Long, colorIndex2 As Long
    
        Select Case target.Value
            Case 1
                colorIndex1 = 4 'green
                colorIndex2 = 3 'red
            Case 2
                colorIndex1 = 3 'red
                colorIndex2 = 4 'green
    
            Case 3 To 5
                colorIndex1 = 5 'blue
                colorIndex2 = 6 'yellow
    
            Case Else
                colorIndex1 = xlColorIndexNone
                colorIndex2 = xlColorIndexNone
        End Select
    
        target.Range("B1,C1,E1,H1").Interior.ColorIndex = colorIndex1
        target.Range("D1,F1,G1,J1").Interior.ColorIndex = colorIndex2
    End Sub
    

如您所见,您可以根据需要更改 colorIndex1colorIndex2 Case Case

此外,单个 Case 可以处理一系列目标值,如 Case 3 To 5 等,让您大大减少打字负担