why does this only work on the worksheet_change event?

rickblunt

Active Member
Joined
Feb 18, 2008
Messages
474
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
 

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,906
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?
 

rickblunt

Active Member
Joined
Feb 18, 2008
Messages
474
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.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,906
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
 

rickblunt

Active Member
Joined
Feb 18, 2008
Messages
474
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.
 

rickblunt

Active Member
Joined
Feb 18, 2008
Messages
474
never mind - I just saw that space between the schemecolor and forecolor - it worked great, thanks again for the help
 

Watch MrExcel Video

Forum statistics

Threads
1,122,514
Messages
5,596,610
Members
414,080
Latest member
penguin23

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
Top