如何在VBA中移动新创建的图表

时间:2015-01-22 08:09:08

标签: excel vba charts

我有一个程序可以创建一系列图表,这些图表似乎是在他们自己的工作表上创建的。然后重命名工作表。之后,我想将所有新创建的工作表移动到新的工作簿中。问题是,当我循环工作簿中的工作表时,似乎没有新的工作表在循环中被拾取。在Project explorer中,它将它们显示为图表而非工作表。如何循环使用它们并将它们移动到新的工作簿中,因为它不会将它们作为工作表进行拾取?

由于



    BeneficiaryNameFind = BeneficiaryList(i)
    If InStr(1, BeneficiaryList(i), "/") > 0 Then
        BeneficiaryList(i) = Replace(BeneficiaryList(i), "/", "")
    End If
    If BeneficiaryList(i) <> BeneficiaryList(i - 1) Then
      BeneficiaryName = Left(BeneficiaryList(i), 30)
      'Adding new worksheet
      Worksheets("DataforPrincipals").Activate
      Set wsnew = Worksheets.Add
      wsnew.Name = BeneficiaryName
      Worksheets(BeneficiaryName).Activate
      'Creating Pivot table
      Set objPivotTable = objPivotcache.CreatePivotTable(wsnew.Range("A1"))
      'set Beneficiary row field
      'Setting Fields
      With objPivotTable
      With .PivotFields("Business Name")
        .Orientation = xlPageField
        .CurrentPage = "ALL"
        .ClearAllFilters
        .CurrentPage = BeneficiaryNameFind
      End With
     'set data fields (PI TO, TO)
      .AddDataField .PivotFields("Pre-ignition T/O"), "PI T/O", xlSum
      .AddDataField .PivotFields("ITD Average"), "ITD", xlSum
      .AddDataField .PivotFields("Growth"), "TO Growth", xlSum
      With .PivotFields("PI T/O")
        .NumberFormat = "$ #,##0"
      End With
      With .PivotFields("ITD")
       .NumberFormat = "$ #,##0"
      End With
      With .PivotFields("TO Growth")
       .NumberFormat = "#%"
      End With
      End With
      ' Access the new PivotTable from the sheet's PivotTables collection.
      Set objPivot = ActiveSheet.PivotTables(1)
      ' Add a new chart sheet.
      Set objChart = Charts.Add
      ' Create a Range object that contains
      ' all of the PivotTable data, except the page fields.
      Set objPivotRange = objPivot.TableRange1
      ' Specify the PivotTable data as the chart's source data.
      With objChart
        .ShowAllFieldButtons = False
        .SetSourceData objPivotRange
        .ChartType = xlColumnClustered
     '   .HasDataTable = True
        .SeriesCollection(1).HasDataLabels = True
        .SeriesCollection(1).DataLabels.NumberFormat = "$ #,##0"
        .SeriesCollection(1).DataLabels.Font.Size = 14
        With .SeriesCollection(1).DataLabels.Format.Fill
          .Visible = msoTrue
          .ForeColor.ObjectThemeColor = msoThemeColorAccent1
          .ForeColor.TintAndShade = 0
          .ForeColor.Brightness = 0.6000000238
          .Transparency = 0
          .Solid
        End With
        .SeriesCollection(2).HasDataLabels = True
        .SeriesCollection(2).DataLabels.NumberFormat = "$ #,##0"
        .SeriesCollection(2).DataLabels.Font.Size = 14
        With .SeriesCollection(2).DataLabels.Format.Fill
          .Visible = msoTrue
          .ForeColor.ObjectThemeColor = msoThemeColorAccent2
          .ForeColor.TintAndShade = 0
          .ForeColor.Brightness = 0.6000000238
          .Transparency = 0
          .Solid
        End With
        .SeriesCollection(3).HasDataLabels = True
        .SeriesCollection(3).DataLabels.NumberFormat = "#%"
        .SeriesCollection(3).DataLabels.Font.Size = 14
        With .SeriesCollection(3).DataLabels.Format.Fill
          .Visible = msoTrue
          .ForeColor.ObjectThemeColor = msoThemeColorAccent3
          .ForeColor.TintAndShade = 0
          .ForeColor.Brightness = 0.400000006
          .Transparency = 0
          .Solid
        End With
      End With
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您可以做的是将所有新创建的图表存储到一个集合中,从而保存对工作表的引用:

在For循环之上:

Dim allNewSheets As New Collection

在For循环中:

Set wsnew = Worksheets.Add
allNewSheets.Add wsnew

因此,最后,您将把所有新工作表放入集合中,以便将它们移动到其他地方:

For j = 1 To allNewSheets.Count
    'move allNewSheets(j)
Next j

建议:尝试使用更多参考资料:首选

With wsnew
    'add charts, pivots etc.
End With

而不是

wsnew.Activate
'add charts, pivots etc.

答案 1 :(得分:0)

Excel有几种类型的工作表。

  • 工作表 - 用于存储数据和公式;行,列,单元格, 等
  • 图表表 - 只是一张图表。
  • Macro Sheets - 已过时。
  • Dialog Sheets - 已过时。

现在我们通常会在工作表中添加嵌入式图表,但Charts.Add会添加一个新的图表工作表。在工作表中循环不会找到图表。但是,这将:

For Each cht In ActiveWorkbook.Charts

其中cht被声明为图表或对象。