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?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,816
Office Version
2007
Platform
Windows
Try

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
Whilst I no nothing about charts, having never used them. Try
Code:
ActiveChart.SetSourceData Source:=Range("'" & AA & "'!$A$4:$K$300")
 

nardagus

Active Member
Joined
Apr 23, 2012
Messages
280
Hello,

You can also write it in this way:

Code:
ActiveChart.SetSourceData Source:=Sheets("AA").Range("A4:K300")
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
As AA is a variable, you would need to remove the quotes from it
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,816
Office Version
2007
Platform
Windows
Test without sheet

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,081,975
Messages
5,362,510
Members
400,678
Latest member
alecalec202

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top