Automation Error while adding chart objects
I am getting an Automation error when trying to add a chartobject in VBA. The weird thing is if I try the same thing with much less data, it works perfectly, but if the number of data increases (say 100 rows), it generates the automation error. The problem seems to come from this line according to the debugger:
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Here is the full code:
Sub Create_Framework()
Dim Graph As ChartObject
Dim Data As Range
Dim SingleSheet As Worksheet
Application.ScreenUpdating = False
'Creating Net Columns
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G2").Value = "Net Hedgers"
Range("H2").Value = "Net Funds"
'Net Hedgers Formula
Range("G3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -4).Value - ActiveCell.Offset(0, -3).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -5).Value = 0
'Net Funds Formula
Range("H3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -3).Value - ActiveCell.Offset(0, -2).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -6).Value = 0
'Alignment
Columns("A:Z").HorizontalAlignment = xlCenter
'Adding Other Sheets
Worksheets.Add After:=Sheets(Sheets.Count), Count:=6
'Renaming
Worksheets(2).Name = "LHedgers"
Worksheets(3).Name = "SHedgers"
Worksheets(4).Name = "LFunds"
Worksheets(5).Name = "SFunds"
Worksheets(6).Name = "Net Hedgers"
Worksheets(7).Name = "Net Funds"
'Copy Data From Main Sheet To Created Sheets
Worksheets(1).Activate
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Select
Selection.Copy
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
SingleSheet.Range("B2").PasteSpecial
SingleSheet.Columns("A:Z").AutoFit
End If
Next SingleSheet
'Move Data To Selected Rows
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
ActiveWindow.DisplayGridlines = False
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Cut Range("B25")
Cells.Select
Selection.Font.Size = 10
Selection.Font.Name = "Calibri Light"
End If
Next SingleSheet
'Draw Charts & AutoFit Them
Sheets(2).Activate
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Graph.Chart.SetSourceData Source:=Union(Range("C26", Range("C26").End(xlDown)), Range("I26", Range("I26").End(xlDown)))
Graph.Chart.SeriesCollection(2).AxisGroup = xlSecondary
Graph.Chart.HasAxis(xlCategory, xlSecondary) = True
Graph.Chart.ChartType = xlLine
excel vba charts
add a comment |
I am getting an Automation error when trying to add a chartobject in VBA. The weird thing is if I try the same thing with much less data, it works perfectly, but if the number of data increases (say 100 rows), it generates the automation error. The problem seems to come from this line according to the debugger:
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Here is the full code:
Sub Create_Framework()
Dim Graph As ChartObject
Dim Data As Range
Dim SingleSheet As Worksheet
Application.ScreenUpdating = False
'Creating Net Columns
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G2").Value = "Net Hedgers"
Range("H2").Value = "Net Funds"
'Net Hedgers Formula
Range("G3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -4).Value - ActiveCell.Offset(0, -3).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -5).Value = 0
'Net Funds Formula
Range("H3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -3).Value - ActiveCell.Offset(0, -2).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -6).Value = 0
'Alignment
Columns("A:Z").HorizontalAlignment = xlCenter
'Adding Other Sheets
Worksheets.Add After:=Sheets(Sheets.Count), Count:=6
'Renaming
Worksheets(2).Name = "LHedgers"
Worksheets(3).Name = "SHedgers"
Worksheets(4).Name = "LFunds"
Worksheets(5).Name = "SFunds"
Worksheets(6).Name = "Net Hedgers"
Worksheets(7).Name = "Net Funds"
'Copy Data From Main Sheet To Created Sheets
Worksheets(1).Activate
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Select
Selection.Copy
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
SingleSheet.Range("B2").PasteSpecial
SingleSheet.Columns("A:Z").AutoFit
End If
Next SingleSheet
'Move Data To Selected Rows
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
ActiveWindow.DisplayGridlines = False
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Cut Range("B25")
Cells.Select
Selection.Font.Size = 10
Selection.Font.Name = "Calibri Light"
End If
Next SingleSheet
'Draw Charts & AutoFit Them
Sheets(2).Activate
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Graph.Chart.SetSourceData Source:=Union(Range("C26", Range("C26").End(xlDown)), Range("I26", Range("I26").End(xlDown)))
Graph.Chart.SeriesCollection(2).AxisGroup = xlSecondary
Graph.Chart.HasAxis(xlCategory, xlSecondary) = True
Graph.Chart.ChartType = xlLine
excel vba charts
add a comment |
I am getting an Automation error when trying to add a chartobject in VBA. The weird thing is if I try the same thing with much less data, it works perfectly, but if the number of data increases (say 100 rows), it generates the automation error. The problem seems to come from this line according to the debugger:
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Here is the full code:
Sub Create_Framework()
Dim Graph As ChartObject
Dim Data As Range
Dim SingleSheet As Worksheet
Application.ScreenUpdating = False
'Creating Net Columns
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G2").Value = "Net Hedgers"
Range("H2").Value = "Net Funds"
'Net Hedgers Formula
Range("G3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -4).Value - ActiveCell.Offset(0, -3).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -5).Value = 0
'Net Funds Formula
Range("H3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -3).Value - ActiveCell.Offset(0, -2).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -6).Value = 0
'Alignment
Columns("A:Z").HorizontalAlignment = xlCenter
'Adding Other Sheets
Worksheets.Add After:=Sheets(Sheets.Count), Count:=6
'Renaming
Worksheets(2).Name = "LHedgers"
Worksheets(3).Name = "SHedgers"
Worksheets(4).Name = "LFunds"
Worksheets(5).Name = "SFunds"
Worksheets(6).Name = "Net Hedgers"
Worksheets(7).Name = "Net Funds"
'Copy Data From Main Sheet To Created Sheets
Worksheets(1).Activate
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Select
Selection.Copy
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
SingleSheet.Range("B2").PasteSpecial
SingleSheet.Columns("A:Z").AutoFit
End If
Next SingleSheet
'Move Data To Selected Rows
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
ActiveWindow.DisplayGridlines = False
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Cut Range("B25")
Cells.Select
Selection.Font.Size = 10
Selection.Font.Name = "Calibri Light"
End If
Next SingleSheet
'Draw Charts & AutoFit Them
Sheets(2).Activate
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Graph.Chart.SetSourceData Source:=Union(Range("C26", Range("C26").End(xlDown)), Range("I26", Range("I26").End(xlDown)))
Graph.Chart.SeriesCollection(2).AxisGroup = xlSecondary
Graph.Chart.HasAxis(xlCategory, xlSecondary) = True
Graph.Chart.ChartType = xlLine
excel vba charts
I am getting an Automation error when trying to add a chartobject in VBA. The weird thing is if I try the same thing with much less data, it works perfectly, but if the number of data increases (say 100 rows), it generates the automation error. The problem seems to come from this line according to the debugger:
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Here is the full code:
Sub Create_Framework()
Dim Graph As ChartObject
Dim Data As Range
Dim SingleSheet As Worksheet
Application.ScreenUpdating = False
'Creating Net Columns
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G2").Value = "Net Hedgers"
Range("H2").Value = "Net Funds"
'Net Hedgers Formula
Range("G3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -4).Value - ActiveCell.Offset(0, -3).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -5).Value = 0
'Net Funds Formula
Range("H3").Select
Do
ActiveCell.Value = ActiveCell.Offset(0, -3).Value - ActiveCell.Offset(0, -2).Value
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, -6).Value = 0
'Alignment
Columns("A:Z").HorizontalAlignment = xlCenter
'Adding Other Sheets
Worksheets.Add After:=Sheets(Sheets.Count), Count:=6
'Renaming
Worksheets(2).Name = "LHedgers"
Worksheets(3).Name = "SHedgers"
Worksheets(4).Name = "LFunds"
Worksheets(5).Name = "SFunds"
Worksheets(6).Name = "Net Hedgers"
Worksheets(7).Name = "Net Funds"
'Copy Data From Main Sheet To Created Sheets
Worksheets(1).Activate
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Select
Selection.Copy
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
SingleSheet.Range("B2").PasteSpecial
SingleSheet.Columns("A:Z").AutoFit
End If
Next SingleSheet
'Move Data To Selected Rows
For Each SingleSheet In Worksheets
If SingleSheet.Name <> "Main" Then
SingleSheet.Activate
ActiveWindow.DisplayGridlines = False
Range("B2", Range("B2").End(xlDown).Offset(0, 7)).Cut Range("B25")
Cells.Select
Selection.Font.Size = 10
Selection.Font.Name = "Calibri Light"
End If
Next SingleSheet
'Draw Charts & AutoFit Them
Sheets(2).Activate
Set Graph = ActiveSheet.ChartObjects.Add(Left:=400, Top:=100, Width:=390, Height:=250)
Graph.Chart.SetSourceData Source:=Union(Range("C26", Range("C26").End(xlDown)), Range("I26", Range("I26").End(xlDown)))
Graph.Chart.SeriesCollection(2).AxisGroup = xlSecondary
Graph.Chart.HasAxis(xlCategory, xlSecondary) = True
Graph.Chart.ChartType = xlLine
excel vba charts
excel vba charts
edited Nov 23 '18 at 21:52
BigBen
6,3172618
6,3172618
asked Nov 23 '18 at 21:30
Sofienne Sofienne
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453138%2fautomation-error-while-adding-chart-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453138%2fautomation-error-while-adding-chart-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown