为什么我的 excel 图表不会转到 VBA 中的新工作簿

Why is my excel Chart not going to a new workbook in VBA

我有一个 excel 工作簿,里面有值。我正在使用这些值并使用 VBA 创建一个图表。然后将图表保存到与值相同的 excel sheet 上的作品 sheet 中。我需要将图表移动到不同的工作簿。我做错了什么?

代码如下:

Sub createColumnBarChart()
 
    
    'declare object variables to hold references to worksheet, source data cell range, created bar chart, and destination cell range

    Dim myWorksheet As Worksheet
    Dim mySourceData As Range
    Dim myChart As Chart
    Dim myChartDestination As Range
    
    'Variable declaration
    Dim sWorkbook As Workbook
    'Create New Workbook
    Set sWorkbook = Workbooks.Add
    
    
    sWorkbook.SaveAs Filename:="C:\Users\username\Desktop\myFolder\Test.xlsx"
 
    'identify worksheet containing source data and created bar chart
'    Set myWorksheet = ThisWorkbook.Worksheets("ChartData")
    Set myWorkbook = Workbooks("myFile.xlsx").Worksheets("ChartData")
 
    With myWorkbook
 
        'identify source data
        Set mySourceData = .Range("A1:AA26")
 
        'identify chart location
'        Set myChartDestination = .Range("A5:M24")
        Set myChartDestination = Workbooks("Test.xlsx").Worksheets("Sheet1").Range("A4:M24")
'
        'create bar chart
        Set myChart = .Shapes.AddChart(XlChartType:=xlColumnStacked, Left:=myChartDestination.Cells(1).Left, Top:=myChartDestination.Cells(1).Top, Width:=myChartDestination.Width, Height:=myChartDestination.Height).Chart
        Charts.Add
        
        
    End With
 
    'set source data for created bar chart
    myChart.SetSourceData Source:=mySourceData
End Sub

不清楚您是想复制带有作品sheet(及其数据)的图表,还是只复制“图表”(这只是一张图片)。如果您复制 Chart 对象本身,它会 link 回到源工作簿中的原始数据(我猜这不是您想要的)。

您的原始代码创建一个图表 (.Shapes.AddChart),然后创建第二个图表 (Charts.Add),由于您创建新工作簿的方式,它可能是当前活动的工作簿。另外,Chart.Add 创建一个空图表 - 这就是您所看到的。

下面的示例在与数据相同的 sheet 上创建图表,然后显示两个示例来说明如何复制或移动图表(和数据)。

Option Explicit

Sub TestMove()
    Dim myChart As Chart
    Set myChart = CreateChart
    
    Dim newWB As Workbook
    Set newWB = Application.Workbooks.Add
    
    Dim chartWS As Worksheet
    Set chartWS = myChart.Parent.Parent
    chartWS.Move Before:=newWB.Sheets(1)
    'newWB.SaveAs Filename:="C:\Users\username\Desktop\myFolder\Test.xlsx"
End Sub

Sub TestCopyPic()
    Dim myChart As Chart
    Set myChart = CreateChart
    
    Dim newWB As Workbook
    Set newWB = Application.Workbooks.Add
    
    myChart.CopyPicture
    newWB.Sheets(1).Paste
    'newWB.SaveAs Filename:="C:\Users\username\Desktop\myFolder\Test.xlsx"
End Sub

Function CreateChart() As Chart
    Dim dataWS As Worksheet
    Dim theData As Range
    Set dataWS = ThisWorkbook.Sheets("ChartData")
    Set theData = dataWS.Range("A1:AA26")
    
    Dim myChartDestination As Range
    Set myChartDestination = dataWS.Range("A4:M24")
    
    Dim theChart As Chart
    With myChartDestination
        Set theChart = dataWS.Shapes.AddChart2(Left:=.Cells(1).Left, _
                                               Top:=.Cells(1).Top, _
                                               Width:=.Width, _
                                               Height:=.Height).Chart
    End With
    
    With theChart
        .ChartType = xlColumnStacked
        .SetSourceData Source:=theData
    End With
        
    Set CreateChart = theChart
End Function