How to only select chart series that are not <blank series> using VBA?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a chart of 40 data series that automatically adapt to the data points based on a slew of names that I defined, and I'm happy with it except that depending on which series appear in the data table, I would have to unselect all the <blank series> manually. This is the only aspect of the chart that is not automatic, and I wish to write some VBA code that would make that happen. But I'm a total newb at VBA, so I hope someone can help me with this.

I tried to get this done by looking online, but it seems to be a bit complex. But I think I can adapt a basic code to my chart if someone can get me the blueprint. Please let me know if I need to supply any chart info in the form of XL2BB or something.

Thanks much for any input!

[p.s.
I have some code already (that I wrote based on info I found online) in my file that helps me with automatic hide/unhide of a few sheets depending on a dropdown menu selection, so I would need to have the chart code together with this code I already have, and I'm not sure where exactly the new code, if any, would go (before or after)?]
 

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"
The following sub will remove empty series from a graph (the data of an empty serie won't show up anyway, but it will appear in the legend)
You will need a way to regenerate the graph for the next time, like have a template graph available with all the series in it, or VBA to generate the graph from scratch.

VBA Code:
Sub RemoveEmptySeries()
    Dim srS As Series

    '' Check if chart is active
    If ActiveChart Is Nothing Then
        '' There is no active chart
        MsgBox "Please select a chart and try again.", vbExclamation, _
          "No Chart Selected"
        Exit Sub
    End If

    '' Loop through all series
    For Each srS In ActiveChart.SeriesCollection
        If IsEmpty(srS.Values(1)) Then
            srS.Delete
        End If
    Next
End Sub
 
Upvote 0
Thank you for sharing this! It was great to go through this VBA exercise. If you find a way to deal with the legend as well, please share.
 
Upvote 0
The code should remove the series and so the legend for that series should as well disappear
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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