Excel 2016: Worksheet Change Event, repeat for many cells

ajay_gajree

Well-known Member
Joined
Jul 16, 2011
Messages
518
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    If IsNumeric(Target.Value) Then
        If Target.Value < 2 Then
            ActiveSheet.Shapes("srRectangle1").Fill.ForeColor.RGB = RGB(255, 0, 0)
        ElseIf Target.Value >= 2 And Target.Value < 3 Then
            ActiveSheet.Shapes("srRectangle1").Fill.ForeColor.RGB = RGB(255, 192, 0)
        Else
            ActiveSheet.Shapes("srRectangle1").Fill.ForeColor.RGB = RGB(0, 255, 0)
        End If
    End If
End Sub

Hi All

I have the above code which changes the colour of a shape, srRectangle1 based on a cell value, A1

If I want to do the same for shape srRectangle2 based on cell value A2, is there a more efficient way than repeating the entire code and changing those values?
Ultimately I have 50 odd shapes in the same worksheet al looking at different cell values, so looking to avoid 50 change events!

Any help appreciated!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("A1:A50")) Is Nothing Then Exit Sub

    If IsNumeric(Target.Value) Then
        If Target.Value < 2 Then
            ActiveSheet.Shapes("srRectangle" & Target.Row).Fill.ForeColor.RGB = RGB(255, 0, 0)
        ElseIf Target.Value >= 2 And Target.Value < 3 Then
            ActiveSheet.Shapes("srRectangle" & Target.Row).Fill.ForeColor.RGB = RGB(255, 192, 0)
        Else
            ActiveSheet.Shapes("srRectangle" & Target.Row).Fill.ForeColor.RGB = RGB(0, 255, 0)
        End If
    End If

End Sub
 
Upvote 0
Thx a lot Norrie, that works.

A related question, Cells A1:A50 are formula based on Cell B1, so when I change the dropdown in B1 this changes the values in A1:A50 but this doesn't trigger the change in the shapes

Any idea why not?

Thx again!
 
Upvote 0
Code:
Private Sub Worksheet_Calculate()
Dim target As Range
Dim i As Integer
Set target = Range("B3:B8")
    'If Intersect(target, Range("B3:B8")) Is Nothing Then Exit Sub
For i = 3 To 8
    If Range("B" & i) = "R" Then
            ActiveSheet.Shapes("srRectangle" & i - 1).Fill.ForeColor.RGB = RGB(255, 0, 0)
        ElseIf Range("B" & i) = "A" Then
            ActiveSheet.Shapes("srRectangle" & i - 1).Fill.ForeColor.RGB = RGB(255, 192, 0)
        ElseIf Range("B" & i) = "G" Then
            ActiveSheet.Shapes("srRectangle" & i - 1).Fill.ForeColor.RGB = RGB(0, 255, 0)
        Else
            ActiveSheet.Shapes("srRectangle" & i - 1).Fill.ForeColor.RGB = RGB(255, 255, 255)
        End If
Next i
End Sub

This was what I needed :)

Thx Norrie!
 
Upvote 0
The change event won't be triggered when a value is changed as a result of a formula.

What you could do is monitor B1 instead of A1:A50, and when that changes loop through A1:A50.

Something like this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cl As Range

    If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub

    For Each cl In Range("A1:A50")

        If IsNumeric(cl.Value) Then
            If cl.Value < 2 Then
                ActiveSheet.Shapes("srRectangle" & cl.Row).Fill.ForeColor.RGB = RGB(255, 0, 0)
            ElseIf cl.Value >= 2 And cl.Value < 3 Then
                ActiveSheet.Shapes("srRectangle" & cl.Row).Fill.ForeColor.RGB = RGB(255, 192, 0)
            Else
                ActiveSheet.Shapes("srRectangle" & cl.Row).Fill.ForeColor.RGB = RGB(0, 255, 0)
            End If
        End If

    Next cl

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top