VBA Chart colour change using case select and wild card

yhtomitl

New Member
Joined
Aug 6, 2019
Messages
3
Hi all VBA experts out there, I have a chart where a macro is able to change the series color using case select.
However when I try to introduce wild card into this case select, I am hitting a road block.

Below is what I got working, as you can see I would like to introduce a wild card instead of specifying every case "ABC", "Abc 123", etc

I have also posted my failed attempt at introducing the wildcard. It would be great to understand exactly why it doesn't work.
Error occurs at For Each rng In ActiveChart.SeriesCollection(x).Name

Code:
Sub ChangeMarqChartLColour()

Dim x As Integer
Dim i As Integer


ActiveSheet.ChartObjects("Marq_ChartL").Activate
i = ActiveChart.SeriesCollection.Count


For x = 1 To i
Select Case ActiveChart.SeriesCollection(x).Name


 Case "ABC", "Abc 123", "Abc 234", "Abc 345", "Abc 456", "Abc 567", "Abc 678", "Abc789"
 ActiveChart.SeriesCollection(x).Format.Line.ForeColor.RGB = RGB(255, 0, 0)


 Case "CCC", "Ccc 123", "Ccc 234"
 ActiveChart.SeriesCollection(x).Format.Line.ForeColor.RGB = RGB(49, 134, 155)
 
 Case Else
 ActiveChart.SeriesCollection(x).Format.Line.ForeColor.RGB = RGB(0, 0, 0)
 
End Select
  
Next
Next
x = Clear
i = Clear




End Sub



'second version

Sub ChangeMarqChartRColour()


Dim x As Integer
Dim i As Integer
Dim rng As Range


ActiveSheet.ChartObjects("Marq_ChartR").Activate
i = ActiveChart.SeriesCollection.Count


For x = 1 To i
For Each rng In ActiveChart.SeriesCollection(x).Name


Select Case True


 Case rng.Value Like "*ABC*"
 ActiveChart.SeriesCollection(x).Format.Line.ForeColor.RGB = RGB(255, 0, 0)


 Case rng.Value Like "*CCC*"
 ActiveChart.SeriesCollection(x).Format.Line.ForeColor.RGB = RGB(49, 134, 155)


 Case Else
 ActiveChart.SeriesCollection(x).Format.Line.ForeColor.RGB = RGB(0, 0, 0)
 
End Select
  
Next
Next
x = Clear
i = Clear




End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try something like this...

Code:
[color=darkblue]Sub[/color] ChangeMarqChartLColour()
    
    [color=darkblue]Dim[/color] x [color=darkblue]As[/color] [color=darkblue]Integer[/color]
    
    ActiveSheet.ChartObjects("Marq_ChartL").Activate
    
    [color=darkblue]For[/color] x = 1 [color=darkblue]To[/color] ActiveChart.SeriesCollection.Count
    
        [color=darkblue]With[/color] ActiveChart.SeriesCollection(x)
            
            [color=darkblue]Select[/color] [color=darkblue]Case[/color] U[color=darkblue]Case[/color](Left(.Name, 3))
                
                [color=darkblue]Case[/color] "ABC": .Format.Line.ForeColor.RGB = RGB(255, 0, 0)
                [color=darkblue]Case[/color] "CCC": .Format.Line.ForeColor.RGB = RGB(49, 134, 155)
                Case Else: .Format.Line.ForeColor.RGB = RGB(0, 0, 0)
                    
            [color=darkblue]End[/color] [color=darkblue]Select[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Next[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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