PDA

View Full Version : excel vba multiple charts in new sheet



lucass3231
08-01-2016, 02:25 AM
I have an issue with preparing macro which would select 3 ranges in sheet and then create new sheet with 3 charts from this data. How can I prepare one? Here is what I have at this moment but doesn't work: ActiveChart.Location Where:="Inwestycje wykresy" is highlighted and 'Type missmatch' error occurs. Do I have to also include charts dimensions and position? Here is my code:

Sub InwestycjeWykresy()Range("B3:N5").Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Inwestycje").Range("B3:N5"), PlotBy:=xlRows
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Inwestycje wykresy"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Alerty"
End With
Worksheets("Inwestycje").Activate
Range("B6:N7").Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Inwestycje").Range("B6:N7"), PlotBy:=xlRows
ActiveChart.Location Where:="Inwestycje wykresy"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Eskalacje"
End With
Worksheets("Inwestycje").Activate
Range("B8:N10").Select
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Inwestycje").Range("B8:N10"), PlotBy:=xlRows
ActiveChart.Location Where:="Inwestycje wykresy"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Nadzor"
End With
End Sub

p45cal
08-01-2016, 10:54 AM
re type mismatch:
Sub InwestycjeWykresy()
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Inwestycje").Range("B3:N5"), PlotBy:=xlRows
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Inwestycje wykresy"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Alerty"
End With
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Inwestycje").Range("B6:N7"), PlotBy:=xlRows
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Eskalacje"
End With
ActiveChart.Location Where:=xlLocationAsObject, Name:="Inwestycje wykresy"
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Inwestycje").Range("B8:N10"), PlotBy:=xlRows
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Nadzor"
End With
ActiveChart.Location Where:=xlLocationAsObject, Name:="Inwestycje wykresy"
End Sub


Your code puts the first chart on a chart sheet; usually, chart sheets hold only one chart, however, if Michael Caine new that they could hold more than one he'd have told you that 'not a lot of people know that'.
Whether you want these charts on a chart sheet or not, yes, you'll need to adjust their size and position.