Method Range of Object Global Failed Error in VBA macro

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

I am running into a Method 'Range' of object'_Global' failed error in my macro below

Code:
Dim AA as String:    AA = ActiveSheet.Name
ActiveSheet.Activate
Range("A4:K300").Select
ActiveSheet.Shapes.AddChart2(227,xlLine).Select
ActiveChart.SetSourceData Source:=Range("AA!$A$4:$K$300")
ActiveChart.Location Where:=xlLocationAsNewSheet

I am receiving the error on line:

Code:
ActiveChart.SetSourceData Source:=Range("AA!$A$4:$K$300")

I suspect it has something to do with the sheet name and possibly variable type. Any ideas why this might be?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try

Code:
ActiveChart.SetSourceData Source:=Range("'" & AA & "'!$A$4:$K$300")
 
Last edited:
Upvote 0
Whilst I no nothing about charts, having never used them. Try
Code:
ActiveChart.SetSourceData Source:=Range("'" & AA & "'!$A$4:$K$300")
 
Upvote 0
Hello,

You can also write it in this way:

Code:
ActiveChart.SetSourceData Source:=Sheets("AA").Range("A4:K300")
 
Last edited:
Upvote 0
As AA is a variable, you would need to remove the quotes from it
 
Upvote 0
Test without sheet

ActiveChart.SetSourceData Source:=Range("$A$4:$K$300")
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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