why does this only work on the worksheet_change event?

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Greetings, I am using this code on a worksheet to change the fill color on a shape dependent on the value in one of the cells. It works great and so I would like to use the same technique in a different WB; but I want to assign it to a command button instead. But I keep getting an error code and I am not smart enough on VBA to understand why....

This works fine:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$46" Then
    '   Change autoshape color to red depending upon cell value, or blank of no value is entered.
        With ActiveSheet.Shapes("Rectangle 1").Fill.ForeColor
            If Target.value = 0 Then
                .SchemeColor = 1
            ElseIf Target.value >= 422 Then
                .SchemeColor = 50
            ElseIf Target.value >= 1 Then
                .SchemeColor = 10
            Else
                'it must be less than 1
            End If
        End With
    End If
End Sub

This gives me the "runtime 424 - object required" error... And I cannot figure out why. There is a value in cell U66 and there is a shape called "Rectangle 618" on the sheet. The button is on the worksheet that I am referencing so I am not sure what object it cannot find...


Code:
Sub FilterStatus()

If Target.Address = "$U$66" Then
    With ActiveSheet.Shapes("Rectangle 618").Fill.ForeColor
        If Target.value = 0 Then
                .SchemeColor = 1
            ElseIf Target.value >= 422 Then
                .SchemeColor = 50
            ElseIf Target.value >= 1 Then
                .SchemeColor = 10
            Else
                'it must be less than 1
            End If
        End With
    End If
End Sub


I appreciate any assistance at all - thanks, Rick
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
In the non-event code, the object Target is not passed to the procedure. You must specify it.

What do you want to happen when the button is pushed?
 
Upvote 0
Thanks for replying mikerickson. Depending on the numerical value in U66, the rectangle will change colors. if the value is greater than 422 it turns green, from 1 to 422 turns it red, and zero leaves it blank.
 
Upvote 0
Code:
With ActiveSheet
    Select Case Val(Cstr(.Range("U66").Value))
        Case Is = 0
            .Shapes("Rectangle 618").Fill.ForeColor .SchemeColor = 1
        Case Is >= 422
            .Shapes("Rectangle 618").Fill.ForeColor .SchemeColor = 50
        Case Else
            .Shapes("Rectangle 618").Fill.ForeColor .SchemeColor = 10
   End Select
End With
 
Upvote 0
Thanks for helping me, when I run this new code I get the 438 error (object doesn't support this property or method) error on whatever step the cell value calls for.
 
Upvote 0
never mind - I just saw that space between the schemecolor and forecolor - it worked great, thanks again for the help
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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