VBA | For Each Loop

Marissie

New Member
Joined
Jun 14, 2017
Messages
6
Hi everyone! How are you?

I have a workbook where I work with a For Each loop to create a chart on each sheet but now I want to change the .CurrentRegion to let every chart collect data from column A and B but I can't make it work.

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

And maybe you also know how to name the charttitle on each sheet as mentioned in cell B1.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Untested though
Rich (BB code):
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:B" & ws.Range("B" & Rows.Count).End(xlUp).Row)
                .ChartColor = 11
                .HasTitle = True
                .ChartTitle.Caption = ws.Range("B1").Value '"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

See if the above works for you
 
Upvote 0
Thank you very much for your time to help me. Unfortunatelly it doesn't work. The charttitle works perfectly though! When I run the code it still adds the rest of the columns.
 
Upvote 0
I forgot to change that line to
Rich (BB code):
.SetSourceData Source:=ws.Range("A3:B" & ws.Range("B" & Rows.Count).End(xlUp).Row)
Does the above work?
 
Upvote 0
No, sorry. That doesn't work either.
I am new here so don't really know if it's possible to add a piece of my file to show you.
 
Upvote 0

Forum statistics

Threads
1,223,944
Messages
6,175,553
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top