Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
Excel 使用VBA将图例条目标题设置为系列标题_Excel_Vba_Excel Formula - Fatal编程技术网

Excel 使用VBA将图例条目标题设置为系列标题

Excel 使用VBA将图例条目标题设置为系列标题,excel,vba,excel-formula,Excel,Vba,Excel Formula,使用VBA,我希望将图例条目重命名为相应的系列标题。在包含的图片中,我希望“系列1”和“系列2”替换为“pbx”和“alt”。我录制了一个宏以了解如何单独操作,但希望创建一个循环以避免具体引用每个系列标题(例如,如果我在D1和E1添加了一个系列标题,图例将自动包含新标题) ActiveChart.FullSeriesCollection(1).Name = "=Sheet1!$B$1" ActiveChart.FullSeriesCollection(2).Name = "=Sheet1!$C

使用VBA,我希望将图例条目重命名为相应的系列标题。在包含的图片中,我希望“系列1”和“系列2”替换为“pbx”和“alt”。我录制了一个宏以了解如何单独操作,但希望创建一个循环以避免具体引用每个系列标题(例如,如果我在D1和E1添加了一个系列标题,图例将自动包含新标题)

ActiveChart.FullSeriesCollection(1).Name = "=Sheet1!$B$1"

ActiveChart.FullSeriesCollection(2).Name = "=Sheet1!$C$1"

我们需要更多信息。具体来说:

  • 标题的范围是动态的吗?是否总是有两个
  • 收割台的范围是否始终位于B1、C1
  • 是否有多张工作表,或者您只在这张工作表中工作
  • 会有多张图表吗
另外,在我对VBA社区进行尽职调查时,您应该避免使用
ActiveChart
或任何依赖于“活动”的内容,包括
选择

根据评论:

Dim rng as范围
我想我会坚持多久
Set rng=此工作簿。工作表(1)。范围(“b1”,范围(“b1”)。结束(xlToRight))
i=0
对于rng.单元格中的每个单元格
i=i+1
ActiveChart.FullSeriesCollection(i).Name=Cell.Text
下一个细胞

因此,让我们编写一个小程序,可以用于任何常规图表

假设:如果Y值在列中,则名称位于第一个Y值上方的单元格中;如果Y值在行中,则名称位于第一个Y值左侧的单元格中

方法:循环活动图表中的每个系列,从系列公式中提取Y值地址,确定哪个单元格具有名称,并将其应用于系列

Sub ApplyNamesToSeries()
  If Not ActiveChart Is Nothing Then
    With ActiveChart
      Dim srs As Series
      For Each srs In .SeriesCollection
        ' series formula
        Dim sFmla As String
        sFmla = srs.Formula
        ' just the arguments
        sFmla = Mid$(Left$(sFmla, Len(sFmla) - 1), InStr(sFmla, "(") + 1)
        ' split into an array
        Dim vFmla As Variant
        vFmla = Split(sFmla, ",")
        ' Y values are the 3rd argument
        Dim sYVals As String
        sYVals = vFmla(LBound(vFmla) + 2)
        ' the Y value range
        Dim rYVals As Range
        Set rYVals = Range(sYVals)
        ' by row or column?
        If rYVals.Rows.Count > 1 Then
          ' by column, so use cell above column of Y values
          Dim rName As Range
          Set rName = rYVals.Offset(-1).Resize(1)
        ElseIf rYVals.Columns.Count > 1 Then
          ' by row, so use cell to left of Y values
          Set rName = rYVals.Offset(, -1).Resize(, 1)
        Else
          ' one cell only: who knows?
          Set rName = Nothing
        End If
        If Not rName Is Nothing Then
          srs.Name = "=" & rName.Address(, , , True)
        End If
      Next
    End With
  End If
End Sub

另一种方法是弹出一个输入框,以便您可以为活动图表选择一个包含序列名称的区域,然后循环遍历序列和区域,将每个单元格应用于每个序列的名称

Sub SelectRangeAndApplySeriesNames()
  If Not ActiveChart Is Nothing Then
    Dim rNames As Range
    On Error Resume Next
    Set rNames = Application.InputBox( _
        "Select a range that contains series names for your chart.", _
        "Select Range of names", , , , , , 8)
    If Not rNames Is Nothing Then
      ' if a range was selected
      If rNames.Rows.Count = 1 Or rNames.Columns.Count = 1 Then
        ' ignore complications...
        With ActiveChart
        Dim iSrs As Long
        For iSrs = 1 To .SeriesCollection.Count
          If iSrs <= rNames.Cells.Count Then
            .SeriesCollection(iSrs).Name = _
                "=" & rNames.Cells(iSrs).Address(, , , True)
          End If
        Next
        End With
      End If
    End If
  End If
End Sub
Sub-selectRange和applyseriesNames()
如果不是,那么ActiveChart什么都不是
作为范围的暗淡RNA
出错时继续下一步
设置rNames=应用程序输入框(_
“选择包含图表系列名称的区域。”_
“选择名称范围”、、8)
如果不是的话,rNames什么都不是
'如果选择了一个范围
如果rNames.Rows.Count=1或rNames.Columns.Count=1,则
“忽略复杂性。。。
使用活动图表
将ISR视为长
对于iSrs=1到.SeriesCollection.Count

如果iSrs ActiveChart仅来自录制的宏。我正在使用一个变量来引用该图表。不会总是有两个标题。标题的范围将始终定位在B1、C1等位置。将只有一个图表。将只有一个工作表。创建一个定义标题位置的范围。我将使用您提供的示例执行此操作,然后由于我不知道如何设置注释的格式,因此我必须将“Dim Cell As Range”声明为“Dim Cell As Range”才能编译该注释。使用提供的代码,图例条目被重命名为“1”和“2”,所以我认为B1、C1单元格没有被引用。成功了!我用接受的答案编辑了您的原始帖子。如果我正在编写一个用户将应用于活动图表的例程,那么活动图表正是引用图表的正确方式。