runtime error '1004': method 'range' of object '_global' failed

sn281

New Member
Joined
Dec 13, 2011
Messages
26
Hi there, thanks for looking.

I have a simple bit of code that I would like to loop through each column and then graph this.
I have my dates in column A and then B, C, D..... etc are full of numbers. I would like them individually graphed into new sheets.

I have written this code but for some reason it fails (see comment on the line that fails).
I have declared the worksheet and when I hover over it knows that ".Name" is equal to "Economic_P&L" (the sheet name) and then the .Address has the correct "$B$2:$B$227", but it just won't run.

Can anyone help? And why is it that it does this? So I can learn :)

Thanks,
sn281


Code:
Sub Create_Graphs()


Dim ws As Worksheet
Dim ch As Chart
Dim rng As Range
Dim i As Long


Set ws = ThisWorkbook.Sheets("Economic_P&L")
Set rng = ws.Range("$B$2:$B$227")


For i = 0 To 39
With ws
    Set ch = .Shapes.AddChart.Chart.Location(xlLocationAsNewSheet, .Range("B2").Offset(0, i))
    ch.ChartType = xlLine
    ch.SetSourceData Source:=Range(.Name & "!" & rng.Offset(0, i).Address) 'fails on this line
    ch.SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$227"


End With
Next


Set rng = Nothing
Set ws = Nothing


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Source:=.Range(.Name & "!" & rng.Offset(0, i).Address)
 
Upvote 0
sn281,

Try adding a . immediately in front of Range

ch.SetSourceData Source:=.Range(.Name & "!" & rng.Offset(0, i).Address)
 
Last edited:
Upvote 0
Adding a "." now gives an error of: [h=2]runtime error '1004': method 'range' of object '_Worksheet' failed[/h]
 
Upvote 0
Try this:

Code:
ch.SetSourceData Source:=rng.Offset(0, i)
 
Upvote 0
Thanks so much all for your replies. That last post from RoryA has solved the issue. I don't fully understand why, but thanks a lot!
sn281
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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