Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何在PowerPoint图表中设置类别/系列名称?_Vba_Powerpoint - Fatal编程技术网

Vba 如何在PowerPoint图表中设置类别/系列名称?

Vba 如何在PowerPoint图表中设置类别/系列名称?,vba,powerpoint,Vba,Powerpoint,我正在编写一个宏来设置类别/系列名称。新标签取自一些词典。然而,我的第一步是从每个图表中获取这些名称。因此,为了得到它,我使用了下面的代码: 'works fine 'x - any integer 'whole sentence Application.ActivePresentation.Slides(x).Shapes(x).Chart.ChartGroups(x).CategoryCollection(x).Name 'with variables Dim objChart As Ch

我正在编写一个宏来设置类别/系列名称。新标签取自一些词典。然而,我的第一步是从每个图表中获取这些名称。因此,为了得到它,我使用了下面的代码:

'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
我在Microsoft Docs()上多次检查此属性(
Name
),它是一个读/写属性。但是,当我尝试设置一个新名称(在下面的代码中)时,我收到一个错误。文档中可能有错误吗

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
我找到了一个解决方法(
SeriesCollection(x).Values
.XValues
),但使用起来并不方便所以,我的问题是:是否存在使用
Name
设置新标签的方法?如果没有-您知道其他解决方法吗?


编辑:我终于设法通过
objChart.ChartData.Workbook
更改了标签,但速度非常慢,更重要的是,必须激活(打开)工作簿才能刷新类别/系列名称。这极大地影响了性能和执行时间,因此我仍在寻找其他方法。

您的第一个列表试图设置.CategoryCollection(1)的名称。但是您链接到的帮助页面是关于.ChartCategory

在任何情况下,以下是如何设置类别名称:

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

我编辑了我的第一个列表来解释
.CategoryCollection(x)
.ChartCategory
之间的联系。
objCategory.Name=“Example”
可能存在什么问题?图表中有4个可见标签源:类别、系列、图例和数据标签。如果图表中不存在某些轴,则类别/系列值可能会显示在图例或数据标签中。毕竟,标签总是取自类别或系列。这就是我需要在集合中显式更改cats/ser的原因。如果cats/ser轴不存在,更改轴上的标签将导致错误。我发布的行设置类别集合的名称,无论该轴是否可见。Microsoft文档中错误百出,因此该属性可能是只读的。VBA文档托管在GitHub上。下面是您可以建议修复此错误的页面:我在遇到最多的两种情况下尝试了您的解决方案(
Axes(enum).CategoryNames
)。我的结果:1)序列轴不存在且标签位于图例中-错误(5):过程调用或参数无效;2) 类别轴不存在且标签位于数据标签中:对象“axis”的方法“CategoryNames”失败。无论如何,我会在GitHub上提交一个问题。