Vba create embedded chart on each worksheet problem

Human_doing

Board Regular
Joined
Feb 16, 2011
Messages
137
Hi all,

Can anyone please take a look at the below code, which is supposed to cycle through each worksheet on a workbook and add a chart based on range M1:N10? The macro doesn't actually seem to do anything, any help much appreciated,

Thanks

Code:
Sub Chart1()
Dim w As Worksheet
For Each w In ThisWorkbook.Worksheets
    Range("M1:N10").Select
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=w.Range("M1:N10"), PlotBy:= _
        xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:=w.Name
Next w
 
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about putting w.Activate after the start of the loop?
 
Upvote 0
You can't select things from sheets that are not active.

Try:
Code:
Dim WS As Worksheet
Dim Rng As Range
For Each WS In Worksheets
        Set Rng = WS.Range("N1:R10")
    With WS.ChartObjects.Add _
            (Left:=Rng.Left, Width:=Rng.Width, Top:=Rng.Top, Height:=Rng.Height)
        .Chart.SetSourceData Source:=WS.Range("M1:M10")
        .Chart.ChartType = xlColumnClustered
    End With
Next WS
 
Upvote 0

Forum statistics

Threads
1,224,579
Messages
6,179,656
Members
452,934
Latest member
mm1t1

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