VBA-Change color of cells based on value in particular cell

后端 未结 1 418
萌比男神i
萌比男神i 2020-12-10 09:34

I want to change the background colors of cells A2:C2 based on the value of cell D2.

This also applies to the relative cells in rows 3,4, a

相关标签:
1条回答
  • 2020-12-10 10:09

    You should use Conditional formatting, but this works:

    Sub ColorMeElmo()
       Dim i As Long, r1 As Range, r2 As Range
    
       For i = 2 To 5
          Set r1 = Range("D" & i)
          Set r2 = Range("A" & i & ":C" & i)
          If r1.Value = 1 Then r2.Interior.Color = vbRed
          If r1.Value = 2 Then r2.Interior.Color = vbBlue
          If r1.Value = 3 Then r2.Interior.Color = vbYellow
       Next i
    End Sub
    
    0 讨论(0)
提交回复
热议问题