Home > Back-end >  Programmatically select X-axis labels within the select data source box of an Excel chart
Programmatically select X-axis labels within the select data source box of an Excel chart

Time:01-22

First off, I don't have any code to show because I don't know how to do what I am trying to do. And when I run the macro recorder, the steps I need are not recorded.

What I am trying to do is to programmatically "click the check box" next to the X-axis labels in the Select Data Source dialog box in an Excel chart. I have a range on a sheet that has each month listed in column B and a total listed in column C. The totals are VLOOKUP functions from a sheet named for the specific month. The chart has the month and total listed as a simple bar. So, you right click in the chart and choose Select Data from the menu. When the dialog box opens, the horizontal axis has each month listed with a check box. There is just a single series. I need to find a way to check a month box using VBA. I was just wondering if there even is a way to do this.

CodePudding user response:

This lists the categories and shows their display statuses. Set IsFiltered = False to display, True to hide.

Sub ListChartCategories()
    Dim FullCategoryCollection As CategoryCollection
    Dim ChartCategory As ChartCategory
    Dim n As Long
    
    Set FullCategoryCollection = ActiveSheet.ChartObjects("Chart 3").Chart.ChartGroups(1).FullCategoryCollection
    
    For n = 1 To FullCategoryCollection.Count
        Set ChartCategory = FullCategoryCollection(n)
        Debug.Print ChartCategory.Name, ChartCategory.IsFiltered
    Next
End Sub
  •  Tags:  
  • Related