Change shape colour based on value button

zzzxy42

New Member
Joined
Jul 11, 2017
Messages
1
Hello!

I am trying to create a macro in which the color changes on a shape depending on a value entered. My sheet has three circle shapes that is corresponding on a factor (Time, Speed and Accuracy). Currently the code works on a worksheet, but it seems that I got the logic wrong. Because sometimes there are inconsistencies about the shape colour. Maybe I got the condition wrong? Here's the guide for the color changes:

TimeSpeedAccuracy
Green
189.5-500

<tbody>
</tbody>
49.5-100

<tbody>
</tbody>
5

<tbody>
</tbody>
Yellow
185-189.4

<tbody>
</tbody>
Red
<=185.4

<tbody>
</tbody>
<=49.4

<tbody>
</tbody>
<=3.5

<tbody>
</tbody>

<tbody>
</tbody>

Here's the current code:

Code:
Private Sub WorksheetChange(ByVal Target As Range)
If Not Intersect(Target, Range("B5")) Is Nothing Then
        Me.Shapes("Oval 1").Select
        With Range("B5")
            If .Value >= 189.5 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbGreen
            ElseIf .Value <= 185 And .Value >= 189.4 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbYellow
            ElseIf .Value <= 185.4 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbRed
            End If
            .Select
        End With
    End If
    If Not Intersect(Target, Range("B6")) Is Nothing Then
        Me.Shapes("Oval 2").Select
        With Range("B6")
           If .Value >= 49.5 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbGreen
            ElseIf .Value < 49.5 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbRed
            End If
            .Select
        End With
    End If
    If Not Intersect(Target, Range("B7")) Is Nothing Then
        Me.Shapes("Oval 3").Select
        With Range("B7")
            If .Value > 5  Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbGreen
            ElseIf .Value <5
                Selection.ShapeRange.Fill.ForeColor.RGB = vbRed
            End If
            .Select
        End With
    End If
End Sub

I would like to put the code on a button so whenever I press on it the color changes after I filled out the cells, but when I added it on a button an error appeared. "Procedure declaration does not match description of event or procedure having same name"

Here's the code I tried after I added a button
Code:
Private Sub CommandButton1_Click(ByVal Target As Range)
If Not Intersect(Target, Range("B5")) Is Nothing Then
        Me.Shapes("Oval 1").Select
        With Range("B5")
            If .Value >= 189.5 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbGreen
            ElseIf .Value <= 185 And .Value >= 189.4 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbYellow
            ElseIf .Value <= 185.4 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbRed
            End If
            .Select
        End With
    End If
    If Not Intersect(Target, Range("B6")) Is Nothing Then
        Me.Shapes("Oval 2").Select
        With Range("B6")
           If .Value >= 49.5 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbGreen
            ElseIf .Value < 49.5 Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbRed
            End If
            .Select
        End With
    End If
    If Not Intersect(Target, Range("B7")) Is Nothing Then
        Me.Shapes("Oval 3").Select
        With Range("B7")
            If .Value > 5  Then
                Selection.ShapeRange.Fill.ForeColor.RGB = vbGreen
            ElseIf .Value <5
                Selection.ShapeRange.Fill.ForeColor.RGB = vbRed
            End If
            .Select
        End With
    End If
End Sub

Any thoughts in fixing this macro would be greatly appreciated. Thank you!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,214,523
Messages
6,120,038
Members
448,940
Latest member
mdusw

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