Soren V. Raben
Soren V. Raben

Reputation: 352

How to SET category/series name in PowerPoint charts?

I'm writing a macro to set categories/series names. New labels are taken from some dictionaries. My first step however was to get those names from each chart. So, in order to get it, I used the code below:

'works fine
'x - any integer
'whole sentence
Application.ActivePresentation.Slides(x).Shapes(x).Chart.ChartGroups(x).CategoryCollection(x).Name

'with variables
Dim objChart As Chart
Dim objChartGroups As ChartGroups
Dim objChartGroup As ChartGroup
Dim objCategoriesColl As CategoryCollection
Dim objCategory As ChartCategory
Dim strCategory As String

Set objChart = ActivePresentation.Slides(x).Shapes(x).Chart
Set objChartGroups = objChart.ChartGroups(x)
Set objChartGroup = objChartGroups.Item(x)
Set objCategoriesColl = objChartGroup.CategoryCollection
Set objCategory = objCategoriesColl(x)
strCategory = objCategory.Name

I checked this property (Name) many times on Microsoft Docs (https://learn.microsoft.com/office/vba/api/powerpoint.chartcategory.name) and it is stated that it is a read/write property. However, when I try to set a new name (in the code below) I receive an error. Is it possible there's a mistake in Docs?

Public Sub EnterNewChartCats( _
  lngCategoriesCount As Long, _
  objCategoriesColl As CategoryCollection, _
  dctDT As Dictionary, _
  dctConsts As Dictionary, _
  dctRegexes As Dictionary, _
  dctUniques As Dictionary)

  'lngCategoriesCount - number of categories in a chart
  'objCategoriesColl - CategoryCollection object (a list of categories)
  'dctDT - a dictionary for names where there's no need to change the label
  'dctConsts - a dictionary for non-unique names which appear many times in a presentation
  'dctRegexes - a dictionary for names which can be replaced using regular expressions
  'dctUniques - a dictionary for names taken from an Excel file (already done)
  
  'booleans for check if a name appears in a dictionary
  Dim blnInDT As Boolean
  Dim blnInConsts As Boolean
  Dim blnInRegexes As Boolean
  Dim blnInUniques As Boolean
  Dim blnAdd As Boolean

  'creating standard variables
  Dim objCategory As ChartCategory
  Dim i As Long
  Dim strCategory As String
  Dim strUCaseToCheck As String
  
  For i = 1 To lngCategoriesCount
  
    Set objCategory = objCategoriesColl(i)
  
    strCategory = Trim(objCategory.Name)
    
    'trimmed and upper-case category string to compare with dictionary keys
    strUCaseToCheck = UCase(strCategory) 
    
    'checking if a category appears in a dictionary
    blnInDT = blnInDct(dctDT, strUCaseToCheck)
    blnInConsts = blnInDct(dctConsts, strUCaseToCheck)
    blnInRegexes = blnAnyValueLikeDctRegex(strUCaseToCheck, dctRegexes)
    blnInUniques = blnInDct(dctUniques, strUCaseToCheck)
    
    'checking if a category meets a condition - at least 1 occurrence in any dictionary
    blnAdd = blnAnyValue(True, blnInConsts, blnInRegexes, blnInUniques)

    'blnAlphabeticChars - a function to check if a category has any letters because
    'there's no need to change such labels as '2021' etc.
    If Not blnInDT And blnAdd And blnAlphabeticChars(strCategory) Then

      'ERROR (objCategory.Name): Can't assign to read-only property
      If blnInConsts Then
        objCategory.Name = dctConsts(strUCaseToCheck)
      ElseIf blnInRegexes Then
        objCategory.Name = dctRegexes(strUCaseToCheck)
      ElseIf blnInUniques Then
        objCategory.Name = dctUniques(strUCaseToCheck)
      End If
    End If
  
  Next i
  
  Set objCategory = Nothing

End Sub

I've found a workaround (SeriesCollection(x).Values or .XValues) but it's not convenient to use. So, my question is: does exist any way to use Name to set new labels? If no - do you know other workarounds?

EDIT: I've finally managed to change labels via objChart.ChartData.Workbook but it's very slow and, more importantly, a workbook has to be activated (opened) in order to refresh categories/series names. That influenced performance and time of execution greatly so I'm still looking for any other way to do so.

Upvotes: 0

Views: 269

Answers (1)

John Korchok
John Korchok

Reputation: 4913

Your first listing is trying to set the name of .CategoryCollection(1). But the help page that you linked to is about .ChartCategory.

In any case, here's how to set the category names:

ActivePresentation.Slides(1).Shapes(1).Chart.Axes(xlCategory).CategoryNames = Array("Test1", "Test2", "Test3", "Test4")

Upvotes: 1

Related Questions