如果值会发生变化,请更改几个单元格颜色

时间:2017-01-16 07:29:32

标签: excel vba cells

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

  • 如果列A中的值将更改 - 运行VBA脚本
  • 例如,如果在单元格A2或A3或A4等中= 1,(单元格B2,C2,E2,H2)将变为绿色并且(D2,F2,G2和J2)将腐烂。 如果A2或A3 ...... = 2(B2,C2,)将为绿色,D2,F2将为

如果A3值改变,则改变B3,C3 如果A4会改变,改变B4,C4等等

A列中的值将手动改变&#34; <#34;

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

以及如何做到这一点?

2 个答案:

答案 0 :(得分:2)

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

但是由于您澄清了单元格将由用户“手动”更改,那么您可以如下所示:

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

如您所见,您可以根据需要随时更改CasecolorIndex1每个colorIndex2

此外,单个Case可以处理一系列目标值,例如Case 3 To 5等等,让您显着降低打字负担

答案 1 :(得分:0)

首先将代码移至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