Change node shape colour based on text within shape

OllieHosking

New Member
Joined
Oct 1, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have the below vba that is used to colour a smartart based on level value.
"
Sub Colour()
Dim oSA As SmartArt
Dim L As Long
'smart art must be selected!
ActiveSheet.Shapes.Range(Array("Diagram 1")).Select
Set oSA = ActiveWindow.Selection.ShapeRange(1).SmartArt
For L = 1 To oSA.AllNodes.Count
Select Case oSA.AllNodes(L).Level
Case Is = 1
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(178, 28, 26) 'Red
Case Is = 2
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(28, 95, 170) 'Blue
Case Is = 3
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(144, 168, 46) 'Green
Case Is = 4
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(230, 130, 3) 'Orange
Case Is = 5
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(250, 118, 136) 'Light pink
Case Is = 6
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(102, 178, 255) 'Light Blue
Case Is = 7
oSA.AllNodes(L).Shapes(1).Fill.ForeColor.RGB = RGB(204, 255, 153) 'Light green
'etc for other levels

End Select
Next L
End Sub
"
Which creates a hierarchy and this is one of the nodes
1686665155434.png


But I want the shape colour to be based of "CS08", with "CS08" having the possibility of being "CS01","CS02","CS03" etc, and the colour will be based of that. Sounds like a lot of case or IF statements, but i would really appreciate it as I can't find anything online!


If you need any more info or an example excel please let me know.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe something like this...

VBA Code:
Sub test()

    Dim theSmartArt As SmartArt
    Set theSmartArt = ActiveSheet.Shapes("Diagram 1").SmartArt
   
    Dim nodeIndex As Long
    Dim nodeText As String
    For nodeIndex = 1 To theSmartArt.AllNodes.Count
        nodeText = theSmartArt.AllNodes(nodeIndex).TextFrame2.TextRange.Text
        If InStr(1, nodeText, "CS01", vbTextCompare) > 0 Then
                theSmartArt.AllNodes(nodeIndex).Shapes(1).Fill.ForeColor.RGB = RGB(178, 28, 26) 'Red
        ElseIf InStr(1, nodeText, "CS02", vbTextCompare) > 0 Then
                theSmartArt.AllNodes(nodeIndex).Shapes(1).Fill.ForeColor.RGB = RGB(28, 95, 170) 'Blue
        '
        '
        '
        End If
    Next nodeIndex
   
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,124
Members
449,097
Latest member
mlckr

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