Hi everyone! How are you?
I am working on a financial report with +100 worksheets where I use a For Each loop to create charts on every worksheet.
I had one that worked excellent but now that I added some extra columns to the pivottable which I don't want to add into the chart, I had to change the sourcedata to get from column A and B instead using currentregion but can't make it work. It's probably a little detail or something but it's frustrating and hope someone can help me.
And maybe you also know how to name the charttitle on each sheet as mentioned in cell B1?
These are the lines of codes that I have now:
Sub CreateCharts()
Dim ws As Worksheet
Dim co As ChartObject
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Inhoudsopgave" Then
Set co = ws.ChartObjects.Add(300, 30, 600, 300)
With co.Chart
.ChartType = xlColumnClustered
.SetSourceData ws.range("A3:B3").End(xlDown))
.ChartColor = 11
.HasTitle = True
.ChartTitle.Caption = "Monthly Sales 2017"
.ChartGroups(1).VaryByCategories = True
.ChartArea.RoundedCorners = True
.ChartArea.Shadow = True
End With
'Move chart
co.Left = ws.range("F3").Left
co.Top = ws.range("F3").Top
co.Width = ws.range("F3:P3").Width
co.Height = ws.range("F3:P22").Height
Set co = Nothing
End If
Next ws
End Sub
I am working on a financial report with +100 worksheets where I use a For Each loop to create charts on every worksheet.
I had one that worked excellent but now that I added some extra columns to the pivottable which I don't want to add into the chart, I had to change the sourcedata to get from column A and B instead using currentregion but can't make it work. It's probably a little detail or something but it's frustrating and hope someone can help me.
And maybe you also know how to name the charttitle on each sheet as mentioned in cell B1?
These are the lines of codes that I have now:
Sub CreateCharts()
Dim ws As Worksheet
Dim co As ChartObject
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Inhoudsopgave" Then
Set co = ws.ChartObjects.Add(300, 30, 600, 300)
With co.Chart
.ChartType = xlColumnClustered
.SetSourceData ws.range("A3:B3").End(xlDown))
.ChartColor = 11
.HasTitle = True
.ChartTitle.Caption = "Monthly Sales 2017"
.ChartGroups(1).VaryByCategories = True
.ChartArea.RoundedCorners = True
.ChartArea.Shadow = True
End With
'Move chart
co.Left = ws.range("F3").Left
co.Top = ws.range("F3").Top
co.Width = ws.range("F3:P3").Width
co.Height = ws.range("F3:P22").Height
Set co = Nothing
End If
Next ws
End Sub