Worksheet variable from array in ActiveChart source data

SBF23456

New Member
Joined
Nov 4, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Greetings,

I am running into an error in defining the source data in the active chart on the line:

VBA Code:
ActiveChart.SetSourceData Source:=Range( _
        "A(element)!$A:$A,A(element)!$E:$E,A(element)!$W:$W")

How am I able to define the value currently "A(element)" as the worksheet name in such a way that for each worksheet I am able to produce a chart?
If I can further clarify the question please let me know.

VBA Code:
A = Array("AAL", "F", "X")

For element = LBound(A) To UBound(A)

    ActiveWorkbook.Worksheets(A(element)).Activate
    ActiveWorkbook.ActiveSheet.Range("A:A,E:E,W:W").Select
    ActiveSheet.Shapes.AddChart2(227, xlLine).Select
    ActiveChart.SetSourceData Source:=Range( _
        "A(element)!$A:$A,A(element)!$E:$E,A(element)!$W:$W")
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi & Welcome to MrExcel!

Give this a try and see whether it helps.
VBA Code:
Sub SBF23456()

    Dim A As Variant, Element As Long, NewChart As Shape
    
    A = Array("AAL", "F", "X")
    For Element = LBound(A) To UBound(A)
        With ThisWorkbook.Worksheets(A(Element))
            Set NewChart = .Shapes.AddChart2(227, xlLine)
            NewChart.Chart.SetSourceData Source:=Range(A(Element) & "!$A:$A," & A(Element) & "!$E:$E," & A(Element) & "!$W:$W")
        End With
    Next
End Sub
 
Upvote 0
If I understand correctly you want create chart on each of these sheets with data from these sheets.
In this code, at the moment of adding chart, you are already on sheet A(element) so you do not have to refer this way.
this should work:

VBA Code:
A = Array("AAL", "F", "X")
For element = LBound(A) To UBound(A)
    ActiveWorkbook.Worksheets(A(element)).Activate
    ActiveWorkbook.ActiveSheet.Range("A:A,E:E,W:W").Select
    ActiveSheet.Shapes.AddChart2(227, xlLine).Select
    ActiveChart.SetSourceData Source:=ActiveSheet.Range("$A:$A,$E:$E,$W:$W")
Next element
 
Upvote 0
Using @KOKOSEK's way of referencing, however without activating or selecting anything, my final submission would look like:

VBA Code:
Sub SBF23456()

    Dim A As Variant, Element As Long, NewChart As Shape
    
    A = Array("AAL", "F", "X")
    For Element = LBound(A) To UBound(A)
        With ThisWorkbook.Worksheets(A(Element))
            .Shapes.AddChart2(227, xlLine).Chart.SetSourceData Source:=.Range("$A:$A,$E:$E,$W:$W")
        End With
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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