Hi,
I have a PT but I don't want to do pt chart since it's not as flexible as a regular one. So I copy TableRange1 part of the pt to another location and then create a regular chart based on that data under the pt.
It worked at first but somehow I got a run-time error like :
run-time error '-2147467259 (80004005)';
Method 'SetSourceData' of object _Chart failed
When debug, it spots the statements: SetDataSource...
Then everything is frozen and I have to shut down the application and restart. Why did it work for a while and then failed?
Here is the code:
Sub CopyPartPTData()
Sheets("Sheet2").Activate
Cells(1, "ba").CurrentRegion.Clear
addr = ActiveSheet.PivotTables(1).TableRange1.Offset(1).Address
Range(addr).Copy Cells(1, "ba")
End Sub
Sub UpdateCharts()
Application.ScreenUpdating = False
Dim ChtOb As ChartObject
Dim SourceRng As Range
Sheets("Sheet2").Activate
On Error Resume Next
ActiveSheet.ChartObjects(1).Delete
On Error GoTo 0
kadr = Range(Range("ba1"), Range("ba1").End(xlToRight).End(xlDown)).Address
'kadr = ActiveSheet.PivotTables(1).DataBodyRange.Address
' MsgBox kadr
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.SetSourceData Source:=Range("Sheet2!" & kadr)
.ChartArea.Select
.ChartType = xlLine
.HasTitle = True
.HasLegend = False
.HasDataTable = True
With .DataTable
.Font.Size = 7
End With
End With
ActiveChart.Parent.RoundedCorners = True
Set ChtOb = ActiveChart.Parent
ChtOb.Height = 320
ChtOb.Width = 1000
ChtOb.Top = 116
ChtOb.Left = 2
Cells(1, 1).Activate
Application.ScreenUpdating = True
End Sub
I have a PT but I don't want to do pt chart since it's not as flexible as a regular one. So I copy TableRange1 part of the pt to another location and then create a regular chart based on that data under the pt.
It worked at first but somehow I got a run-time error like :
run-time error '-2147467259 (80004005)';
Method 'SetSourceData' of object _Chart failed
When debug, it spots the statements: SetDataSource...
Then everything is frozen and I have to shut down the application and restart. Why did it work for a while and then failed?
Here is the code:
Sub CopyPartPTData()
Sheets("Sheet2").Activate
Cells(1, "ba").CurrentRegion.Clear
addr = ActiveSheet.PivotTables(1).TableRange1.Offset(1).Address
Range(addr).Copy Cells(1, "ba")
End Sub
Sub UpdateCharts()
Application.ScreenUpdating = False
Dim ChtOb As ChartObject
Dim SourceRng As Range
Sheets("Sheet2").Activate
On Error Resume Next
ActiveSheet.ChartObjects(1).Delete
On Error GoTo 0
kadr = Range(Range("ba1"), Range("ba1").End(xlToRight).End(xlDown)).Address
'kadr = ActiveSheet.PivotTables(1).DataBodyRange.Address
' MsgBox kadr
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.SetSourceData Source:=Range("Sheet2!" & kadr)
.ChartArea.Select
.ChartType = xlLine
.HasTitle = True
.HasLegend = False
.HasDataTable = True
With .DataTable
.Font.Size = 7
End With
End With
ActiveChart.Parent.RoundedCorners = True
Set ChtOb = ActiveChart.Parent
ChtOb.Height = 320
ChtOb.Width = 1000
ChtOb.Top = 116
ChtOb.Left = 2
Cells(1, 1).Activate
Application.ScreenUpdating = True
End Sub