Insert Excel Graphs using Class to define parameters
up vote
2
down vote
favorite
I have a loop in my main sub routine which creates multiple graphs. This is run across a number of sheets, creating the same graphs for each sheet, but with different ranges and other variables.
I have a separate subroutine for each graph (two are shown below, others are similar and can be posted if necessary), however it would seem more logical to organize this better, specifically using one sub with more variables to insert a graph, as I expand and add more graphs.
I can broadly see two solutions:
Make a sub routine with a large case select statement
Create a class to do this
I'm leaning towards the latter, as it seems like a neater way to achieve what I wish. Through what I've read from CPearson and stackoverflow, I can use classes to set/let/get properties, but I'm unsure as to the best way to organize the code overall.
Do I:
Use a method to let/set all properties at once, then insert a graph by
getting all of these
set each property individually in the main sub routine
Call a subroutine to specifically set the values of a class module
Or, leave the class altogether and just:
Use public variables defined in the main/ subroutine
Pass variables to the insert graph sub routine
Code for calling the insert graph sub routines is directly below, followed by the graphs themselves:
For Each b In rngQueries
Set ToPrint = Worksheets(b.Value).Range("A" & Rows.Count).End(xlUp).Offset(3, 0)
b.Offset(0, 3).Value = ToPrint.Address
b.Offset(0, 4).Value = Worksheets(b.Value).Range("A1000").End(xlUp).Address
Select Case b.Offset(0, 2).Value
Case "Today"
InsertBar ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
Case "TimeSeries"
InsertLine ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
End Select
Next
Graphs:
Sub InsertLine(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$C$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Hits & Attempts - (Last 14 Days)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
End With
End sub
Sub InsertBar(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$D$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Receiving Sim Stats - (Today Only)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
.SeriesCollection(3).Name = Range("D" & lngStartRow - 1).Value
End With
End Sub
To summarize, I am currently passing ToPrint, PosTopLeft and PosBottomLeft to the subroutine from a case select in the main sub, and defining some constants in the graphs sub routine, such as graph position, title etc. It seems more logical to organize this better. Which is the neatest, most logical way to proceed?
Finally, I should say that I am relatively new to this board, if the question is not correctly presented, or not appropriate for here, please let me know and I'll update/ remove. Thanks in advance for any help you can give.
vba
bumped to the homepage by Community♦ 54 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
2
down vote
favorite
I have a loop in my main sub routine which creates multiple graphs. This is run across a number of sheets, creating the same graphs for each sheet, but with different ranges and other variables.
I have a separate subroutine for each graph (two are shown below, others are similar and can be posted if necessary), however it would seem more logical to organize this better, specifically using one sub with more variables to insert a graph, as I expand and add more graphs.
I can broadly see two solutions:
Make a sub routine with a large case select statement
Create a class to do this
I'm leaning towards the latter, as it seems like a neater way to achieve what I wish. Through what I've read from CPearson and stackoverflow, I can use classes to set/let/get properties, but I'm unsure as to the best way to organize the code overall.
Do I:
Use a method to let/set all properties at once, then insert a graph by
getting all of these
set each property individually in the main sub routine
Call a subroutine to specifically set the values of a class module
Or, leave the class altogether and just:
Use public variables defined in the main/ subroutine
Pass variables to the insert graph sub routine
Code for calling the insert graph sub routines is directly below, followed by the graphs themselves:
For Each b In rngQueries
Set ToPrint = Worksheets(b.Value).Range("A" & Rows.Count).End(xlUp).Offset(3, 0)
b.Offset(0, 3).Value = ToPrint.Address
b.Offset(0, 4).Value = Worksheets(b.Value).Range("A1000").End(xlUp).Address
Select Case b.Offset(0, 2).Value
Case "Today"
InsertBar ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
Case "TimeSeries"
InsertLine ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
End Select
Next
Graphs:
Sub InsertLine(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$C$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Hits & Attempts - (Last 14 Days)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
End With
End sub
Sub InsertBar(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$D$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Receiving Sim Stats - (Today Only)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
.SeriesCollection(3).Name = Range("D" & lngStartRow - 1).Value
End With
End Sub
To summarize, I am currently passing ToPrint, PosTopLeft and PosBottomLeft to the subroutine from a case select in the main sub, and defining some constants in the graphs sub routine, such as graph position, title etc. It seems more logical to organize this better. Which is the neatest, most logical way to proceed?
Finally, I should say that I am relatively new to this board, if the question is not correctly presented, or not appropriate for here, please let me know and I'll update/ remove. Thanks in advance for any help you can give.
vba
bumped to the homepage by Community♦ 54 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a loop in my main sub routine which creates multiple graphs. This is run across a number of sheets, creating the same graphs for each sheet, but with different ranges and other variables.
I have a separate subroutine for each graph (two are shown below, others are similar and can be posted if necessary), however it would seem more logical to organize this better, specifically using one sub with more variables to insert a graph, as I expand and add more graphs.
I can broadly see two solutions:
Make a sub routine with a large case select statement
Create a class to do this
I'm leaning towards the latter, as it seems like a neater way to achieve what I wish. Through what I've read from CPearson and stackoverflow, I can use classes to set/let/get properties, but I'm unsure as to the best way to organize the code overall.
Do I:
Use a method to let/set all properties at once, then insert a graph by
getting all of these
set each property individually in the main sub routine
Call a subroutine to specifically set the values of a class module
Or, leave the class altogether and just:
Use public variables defined in the main/ subroutine
Pass variables to the insert graph sub routine
Code for calling the insert graph sub routines is directly below, followed by the graphs themselves:
For Each b In rngQueries
Set ToPrint = Worksheets(b.Value).Range("A" & Rows.Count).End(xlUp).Offset(3, 0)
b.Offset(0, 3).Value = ToPrint.Address
b.Offset(0, 4).Value = Worksheets(b.Value).Range("A1000").End(xlUp).Address
Select Case b.Offset(0, 2).Value
Case "Today"
InsertBar ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
Case "TimeSeries"
InsertLine ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
End Select
Next
Graphs:
Sub InsertLine(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$C$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Hits & Attempts - (Last 14 Days)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
End With
End sub
Sub InsertBar(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$D$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Receiving Sim Stats - (Today Only)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
.SeriesCollection(3).Name = Range("D" & lngStartRow - 1).Value
End With
End Sub
To summarize, I am currently passing ToPrint, PosTopLeft and PosBottomLeft to the subroutine from a case select in the main sub, and defining some constants in the graphs sub routine, such as graph position, title etc. It seems more logical to organize this better. Which is the neatest, most logical way to proceed?
Finally, I should say that I am relatively new to this board, if the question is not correctly presented, or not appropriate for here, please let me know and I'll update/ remove. Thanks in advance for any help you can give.
vba
I have a loop in my main sub routine which creates multiple graphs. This is run across a number of sheets, creating the same graphs for each sheet, but with different ranges and other variables.
I have a separate subroutine for each graph (two are shown below, others are similar and can be posted if necessary), however it would seem more logical to organize this better, specifically using one sub with more variables to insert a graph, as I expand and add more graphs.
I can broadly see two solutions:
Make a sub routine with a large case select statement
Create a class to do this
I'm leaning towards the latter, as it seems like a neater way to achieve what I wish. Through what I've read from CPearson and stackoverflow, I can use classes to set/let/get properties, but I'm unsure as to the best way to organize the code overall.
Do I:
Use a method to let/set all properties at once, then insert a graph by
getting all of these
set each property individually in the main sub routine
Call a subroutine to specifically set the values of a class module
Or, leave the class altogether and just:
Use public variables defined in the main/ subroutine
Pass variables to the insert graph sub routine
Code for calling the insert graph sub routines is directly below, followed by the graphs themselves:
For Each b In rngQueries
Set ToPrint = Worksheets(b.Value).Range("A" & Rows.Count).End(xlUp).Offset(3, 0)
b.Offset(0, 3).Value = ToPrint.Address
b.Offset(0, 4).Value = Worksheets(b.Value).Range("A1000").End(xlUp).Address
Select Case b.Offset(0, 2).Value
Case "Today"
InsertBar ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
Case "TimeSeries"
InsertLine ToPrint, b.Offset(0, 3).Value, b.Offset(0, 4).Value
End Select
Next
Graphs:
Sub InsertLine(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$C$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Hits & Attempts - (Last 14 Days)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
End With
End sub
Sub InsertBar(ToPrint As Range, PosTopLeft As String, PosBottomLeft As String)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngStartRow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngEndRow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range("$A$" & CStr(lngStartRow) & ":$D$" & CStr(lngEndRow)).Select
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
With myChart
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 8
.HasTitle = True
.ChartTitle.Text = ToPrint.Worksheet.Name & " Receiving Sim Stats - (Today Only)"
.SeriesCollection(1).Name = Range("B" & lngStartRow - 1).Value
.SeriesCollection(2).Name = Range("C" & lngStartRow - 1).Value
.SeriesCollection(3).Name = Range("D" & lngStartRow - 1).Value
End With
End Sub
To summarize, I am currently passing ToPrint, PosTopLeft and PosBottomLeft to the subroutine from a case select in the main sub, and defining some constants in the graphs sub routine, such as graph position, title etc. It seems more logical to organize this better. Which is the neatest, most logical way to proceed?
Finally, I should say that I am relatively new to this board, if the question is not correctly presented, or not appropriate for here, please let me know and I'll update/ remove. Thanks in advance for any help you can give.
vba
vba
edited Nov 29 '16 at 14:12
asked Nov 29 '16 at 14:07
User632716
9310
9310
bumped to the homepage by Community♦ 54 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 54 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I'm not sure a class is exactly what you want - a chart is already an object with the properties/methods you want. You want to create a sub that takes the arguments you need, like
Sub CreateChart(ByVal targetRange As Range, ByVal PosTopLeft As String, byval PosBottomLeft As String, ByVal lastColumn As Long, ByVal chartType As XlChartType)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngstartrow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngendrow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range(.Cells(lngstartrow, 1), (.Cells(lngendrow, lastColumn))).Select
Select Case XlChartType
Case xlLine
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
Case xlColumnClustered
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
End Select
End Sub
And then you can have some variable that picks the number of series by the type of chart, then add all the series with a loop. You can also have a string that picks the title based on case.
The key is to combine as many parts of the process into a single process. Combining something a little like this -
Case xlLine
numberofseries = 2
Case xlColumnClustered
numberofseries = 3
For i = 1 To numberofseries
.SeriesCollection(i).Name = .Cells(lngstartrow - 1, i + 1).Value
Next
So your case statements don't have to do very much, all they need to do is assign the proper variables to be used in the next parts.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f148448%2finsert-excel-graphs-using-class-to-define-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I'm not sure a class is exactly what you want - a chart is already an object with the properties/methods you want. You want to create a sub that takes the arguments you need, like
Sub CreateChart(ByVal targetRange As Range, ByVal PosTopLeft As String, byval PosBottomLeft As String, ByVal lastColumn As Long, ByVal chartType As XlChartType)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngstartrow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngendrow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range(.Cells(lngstartrow, 1), (.Cells(lngendrow, lastColumn))).Select
Select Case XlChartType
Case xlLine
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
Case xlColumnClustered
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
End Select
End Sub
And then you can have some variable that picks the number of series by the type of chart, then add all the series with a loop. You can also have a string that picks the title based on case.
The key is to combine as many parts of the process into a single process. Combining something a little like this -
Case xlLine
numberofseries = 2
Case xlColumnClustered
numberofseries = 3
For i = 1 To numberofseries
.SeriesCollection(i).Name = .Cells(lngstartrow - 1, i + 1).Value
Next
So your case statements don't have to do very much, all they need to do is assign the proper variables to be used in the next parts.
add a comment |
up vote
0
down vote
I'm not sure a class is exactly what you want - a chart is already an object with the properties/methods you want. You want to create a sub that takes the arguments you need, like
Sub CreateChart(ByVal targetRange As Range, ByVal PosTopLeft As String, byval PosBottomLeft As String, ByVal lastColumn As Long, ByVal chartType As XlChartType)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngstartrow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngendrow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range(.Cells(lngstartrow, 1), (.Cells(lngendrow, lastColumn))).Select
Select Case XlChartType
Case xlLine
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
Case xlColumnClustered
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
End Select
End Sub
And then you can have some variable that picks the number of series by the type of chart, then add all the series with a loop. You can also have a string that picks the title based on case.
The key is to combine as many parts of the process into a single process. Combining something a little like this -
Case xlLine
numberofseries = 2
Case xlColumnClustered
numberofseries = 3
For i = 1 To numberofseries
.SeriesCollection(i).Name = .Cells(lngstartrow - 1, i + 1).Value
Next
So your case statements don't have to do very much, all they need to do is assign the proper variables to be used in the next parts.
add a comment |
up vote
0
down vote
up vote
0
down vote
I'm not sure a class is exactly what you want - a chart is already an object with the properties/methods you want. You want to create a sub that takes the arguments you need, like
Sub CreateChart(ByVal targetRange As Range, ByVal PosTopLeft As String, byval PosBottomLeft As String, ByVal lastColumn As Long, ByVal chartType As XlChartType)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngstartrow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngendrow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range(.Cells(lngstartrow, 1), (.Cells(lngendrow, lastColumn))).Select
Select Case XlChartType
Case xlLine
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
Case xlColumnClustered
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
End Select
End Sub
And then you can have some variable that picks the number of series by the type of chart, then add all the series with a loop. You can also have a string that picks the title based on case.
The key is to combine as many parts of the process into a single process. Combining something a little like this -
Case xlLine
numberofseries = 2
Case xlColumnClustered
numberofseries = 3
For i = 1 To numberofseries
.SeriesCollection(i).Name = .Cells(lngstartrow - 1, i + 1).Value
Next
So your case statements don't have to do very much, all they need to do is assign the proper variables to be used in the next parts.
I'm not sure a class is exactly what you want - a chart is already an object with the properties/methods you want. You want to create a sub that takes the arguments you need, like
Sub CreateChart(ByVal targetRange As Range, ByVal PosTopLeft As String, byval PosBottomLeft As String, ByVal lastColumn As Long, ByVal chartType As XlChartType)
Dim strRange As String
Dim rngChart As Range
Dim myChart As Chart
lngstartrow = Sheets(ToPrint.Worksheet.Name).Range(PosTopLeft).Row
lngendrow = Sheets(ToPrint.Worksheet.Name).Range(PosBottomLeft).Row
Sheets(ToPrint.Worksheet.Name).Activate
Sheets(ToPrint.Worksheet.Name).Range(.Cells(lngstartrow, 1), (.Cells(lngendrow, lastColumn))).Select
Select Case XlChartType
Case xlLine
Set myChart = ActiveSheet.Shapes.AddChart(xlLine, 500, 200).Chart
Case xlColumnClustered
Set myChart = ActiveSheet.Shapes.AddChart(xlColumnClustered, 500, 10, , 175).Chart
End Select
End Sub
And then you can have some variable that picks the number of series by the type of chart, then add all the series with a loop. You can also have a string that picks the title based on case.
The key is to combine as many parts of the process into a single process. Combining something a little like this -
Case xlLine
numberofseries = 2
Case xlColumnClustered
numberofseries = 3
For i = 1 To numberofseries
.SeriesCollection(i).Name = .Cells(lngstartrow - 1, i + 1).Value
Next
So your case statements don't have to do very much, all they need to do is assign the proper variables to be used in the next parts.
answered Mar 22 at 21:41
Raystafarian
5,8241048
5,8241048
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fcodereview.stackexchange.com%2fquestions%2f148448%2finsert-excel-graphs-using-class-to-define-parameters%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