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

我找不到我的问题的答案,也许你可以帮我解决这个问题。 我想改变这个VBA脚本有这样的:

  • 如果在列中的值将改变 – 运行VBA脚本
  • 例如,如果在单元格A2或A3或A4等等= 1中,(单元格B2,C2,E2,H2)将变成绿色,并且(D2,F2,G2和J2)将会变形。 如果A2或A3 …… = 2(B2,C2)将变成绿色,则D2,F2将变质

如果A3值会变化,那么比改变B3,如果A4将改变C3,改变B4,C4等

A列用户的值将“手动”更改

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可以在将颜色修改为“绿色”时添加多个要检查的scheme。

 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 

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

但是既然你澄清了单元格会被用户“手动”改变,那么你可以像下面这样去做:

  • 在“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 

正如你看到的,你可以玩每个Case只是改变colorIndex1colorIndex2根据您的需要

此外,一个Case可以处理一系列的目标值,如Case 3 To 5等,并让你大大减less打字负担