Shape formatting

Corleone

Well-known Member
Joined
Feb 2, 2003
Messages
841
Office Version
  1. 365
Software: Excel365

Hi im using the following code to try and get a shape to change colour based on the text in Cell "C8"
The code is contained in the worksheet tab

however, when testing it out by typing in either Red, green or yellow i get an error message - "wrong number of assignments or invalid property assignment"

Private Sub WORKSHEET_CHANGE(ByVal TARGET As Range)
If Range("C8") = "RED" Then
ActiveSheet.Shapes.Range.Fill(Array("RUGELEY")).Select
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)

Else

If Range("C8") = "GREEN" Then
ActiveSheet.Shapes.Range.Fill(Array("RUGELEY")).Select
Selection.ShapeRange.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent6

Else

If Range("C8") = "YELLOW" Then
ActiveSheet.Shapes.Range.Fill(Array("RUGELEY")).Select
Selection.ShapeRange.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent4

End If
End If
End If

ActiveSheet.Cells(8, 3).Select

End Sub


Thanks
 
Try this:
VBA Code:
Private Sub WORKSHEET_CHANGE(ByVal TARGET As Range)
'Modified  5/19/2022  9:47:41 AM  EDT
If TARGET.Address = "$C$8" Then

Select Case TARGET.Value
    Case "RED"
        ActiveSheet.Shapes.Range(Array("RUGELEY")).Select
        Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)

Case "GREEN"
    ActiveSheet.Shapes.Range(Array("RUGELEY")).Select
    Selection.ShapeRange.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent6

Case "YELLOW"
    ActiveSheet.Shapes.Range(Array("RUGELEY")).Select
    Selection.ShapeRange.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent4

End Select


Cells(8, 3).Select
End If
End Sub
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Shp As String
   Dim Ary As Variant
  
   Ary = Array("RUGELEY", "MACC")
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("C8:C9")) Is Nothing Then
      Shp = Ary(Target.Row - 8)
      Select Case Target.Value
         Case "RED"
            Me.Shapes(Shp).Fill.ForeColor.RGB = RGB(255, 0, 0)
         Case "GREEN"
            Me.Shapes(Shp).Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent6
         Case "YELLOW"
            Me.Shapes(Shp).Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent4
      End Select
   End If
End Sub
 
Upvote 0
Private Sub Worksheet_Change(ByVal Target As Range) Dim Shp As String Dim Ary As Variant Ary = Array("RUGELEY", "MACC") If Target.CountLarge > 1 Then Exit Sub If Not Intersect(Target, Range("C8:C9")) Is Nothing Then Shp = Ary(Target.Row - 8) Select Case Target.Value Case "RED" Me.Shapes(Shp).Fill.ForeColor.RGB = RGB(255, 0, 0) Case "GREEN" Me.Shapes(Shp).Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent6 Case "YELLOW" Me.Shapes(Shp).Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent4 End Select End If End Sub
Yes - This is a more elegant solution to the one i had come up with as i can extend the range without worrying about the order changing if i sort them
once again thanks for your help
 
Upvote 0
I have found a workaround by creating separate macros for each shape and then creating another macro which runs them all simultaneously
Many thanks for your help
I believe this could be done with different shape names all in the same sub.

Show us the new code you are using for the other shapes
 
Upvote 0
as i can extend the range without worrying about the order changing if i sort them
The order of the shape names needs to be the same as the order of the cells, otherwise you will highlight the wrong shape.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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