vbanewb1986
New Member
- Joined
- Nov 4, 2013
- Messages
- 9
Hi everyone,
I need Excel VBA to perform a simple task over and over again. I want to create a line chart using one series (say, Income) that I have given a named range (ChartStartPoint) and a date series that also has a named range associated (ChartDataRange). I will above chart will serve as the blueprint for how the other charts will be formatted.
I have nine more series of data that I want to loop through and create line charts for, while copying the format of the first chart (one of 10). This is my first attempt at a macro using chart objects. I have tried to create charts and and look back through the code; the part I cannot get past is when I want to select the new chart and remove the legend - I am not even sure if the code beyond that works. I have tried to setup a chart object but kept getting error messages as well. Even if I knew how to select a dynamic object, I would have to setup a loop to cycle through each new chart object when I create them (I created the chtcounter variable but the code is giving me problems; any help would be great - thank you).
Sub ReplicateLineChart()
Dim chtcounter As Integer
For chtcounter = 52 To 62
ActiveSheet.range("ChartStartPoint").Select
'Add a chart
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=range("Calculations!$L$12:$W$12")
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection(1).XValues = _
"='FILE NAME.xls'!ChartDataRange"
'Delete the legend
ActiveSheet.objectchart.Legend.Select.Delete
'Select the chart and set the Y axis range
ActiveSheet.ChartObjects("Chart & chtcounter").Activate
'Will always refer to the range below
cht.SeriesCollection(1).XValues = "=Calculations!$M$2:$W$2"
ActiveWindow.SmallScroll Down:=6
'Copy the chart formatting from another chart
ActiveSheet.ChartObjects("Chart19").Activate
cht.ChartArea.Copy
'Select the chart you want to apply the formatting to
ActiveSheet.ChartObjects("Chart & chtcounter").Activate
ActiveSheet.PasteSpecial Format:=2
cht.ChartTitle.Select
cht.ChartTitle.Text = "Company Name" & " Staff Costs: Income (YTD)"
'Before the loop starts over I need the series to move down one row
ActiveSheet.range("ChartStartPoint").Offset(1, 0) = ChartStartPoint
Next chtcounter
End Sub
I need Excel VBA to perform a simple task over and over again. I want to create a line chart using one series (say, Income) that I have given a named range (ChartStartPoint) and a date series that also has a named range associated (ChartDataRange). I will above chart will serve as the blueprint for how the other charts will be formatted.
I have nine more series of data that I want to loop through and create line charts for, while copying the format of the first chart (one of 10). This is my first attempt at a macro using chart objects. I have tried to create charts and and look back through the code; the part I cannot get past is when I want to select the new chart and remove the legend - I am not even sure if the code beyond that works. I have tried to setup a chart object but kept getting error messages as well. Even if I knew how to select a dynamic object, I would have to setup a loop to cycle through each new chart object when I create them (I created the chtcounter variable but the code is giving me problems; any help would be great - thank you).
Sub ReplicateLineChart()
Dim chtcounter As Integer
For chtcounter = 52 To 62
ActiveSheet.range("ChartStartPoint").Select
'Add a chart
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=range("Calculations!$L$12:$W$12")
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection(1).XValues = _
"='FILE NAME.xls'!ChartDataRange"
'Delete the legend
ActiveSheet.objectchart.Legend.Select.Delete
'Select the chart and set the Y axis range
ActiveSheet.ChartObjects("Chart & chtcounter").Activate
'Will always refer to the range below
cht.SeriesCollection(1).XValues = "=Calculations!$M$2:$W$2"
ActiveWindow.SmallScroll Down:=6
'Copy the chart formatting from another chart
ActiveSheet.ChartObjects("Chart19").Activate
cht.ChartArea.Copy
'Select the chart you want to apply the formatting to
ActiveSheet.ChartObjects("Chart & chtcounter").Activate
ActiveSheet.PasteSpecial Format:=2
cht.ChartTitle.Select
cht.ChartTitle.Text = "Company Name" & " Staff Costs: Income (YTD)"
'Before the loop starts over I need the series to move down one row
ActiveSheet.range("ChartStartPoint").Offset(1, 0) = ChartStartPoint
Next chtcounter
End Sub