简体   繁体   中英

Excel Chart range based on the value in a cell in another sheet

I have a sheet (Dashboard) that has multiple Pareto charts, another sheet (Data) brings in the range for each chart via a formula in standard $A$1:$B$2 format.

how do I use these ranges from the Sheet "Data" in the Pareto charts in the "Dashboard"? Chart name is in Data B4 Chart Range is in Data C4 I have code for each chart for troubleshooting below is one from a single chart

Sub FirstChart()
    Dim FirstChartName As String
    Dim FirstChartRange As Range
    
        FirstChartName = Sheets("Data").Range("B4")
        Set FirstChartRange = Worksheets("Data").Range(Sheets("Data").Range("C4").Value)
        Sheets("Dashboard").ChartObjects("FirstChart").Activate
        ActiveChart.ChartArea.Select
        ActiveChart.HasTitle = True
        ActiveChart.ChartTitle.Text = FirstChartName 
        ActiveChart.SetSourceData Source:=FirstChartRange
End Sub

Thanks in advance.

UPDATE: Thanks to @coross24 and @WIL. i have uploaded the file based on their answers to https://gofile.io/d/8HfjQv

It seems you were slightly off when referencing the FirstChartRange parameter. Since the variable was bound as a Range, what you've done is reference the cell C4 as the range, rather than the string within that range, in turn trying to plot the string value within that cell! When running your code, I ran into a type error.
I've amended your code above and tested it out on a singe chart in my workbook and it seems to work okay. I've also early bound your worksheets so you don't have to repeat yourself in your code.

Sub FirstChart()
    Dim FirstChartName As String
    Dim FirstChartRange As String
    Dim shtData As Excel.Worksheet
    Dim shtDashboard As Excel.Worksheet
    Dim chart As Excel.chart
    
    Set shtData = ThisWorkbook.Sheets("Data")
    Set shtDashboard = ThisWorkbook.Sheets("Dashboard")
    
    ' get chart name
    FirstChartName = shtData.Range("B4").Value2
    ' get chart range
    FirstChartRange = shtData.Range("C4").Value2
    ' change data for first chart
    Set chart = shtDashboard.ChartObjects("FirstChart").chart
    With chart
        .HasTitle = True
        .ChartTitle.Text = FirstChartName
        .SetSourceData shtData.Range(FirstChartRange)
    End With
       
End Sub

Good luck!

Try this One

Sub FirstChart()

Dim FirstChartName As String
Dim FirstChartRange As String
Dim shtData As Excel.Worksheet
Dim shtDashboard As Excel.Worksheet
Dim chart As Excel.chart

Set shtData = ThisWorkbook.Sheets("Data")
Set shtDashboard = ThisWorkbook.Sheets("Dashboard")

' get chart name
FirstChartName = shtData.Range("B4").Value2
' get chart range
FirstChartRange = shtData.Range("C4").Value2
' change data for first chart
Set chart = shtDashboard.ChartObjects("FirstChart").chart
With chart
    .HasTitle = True
    .ChartTitle.Text = FirstChartName
    .SetSourceData FirstChartRange
End With
   
End Sub

Relik,
I've had to post another answer as my reputation isn't high enough to reply with a comment. There's an absolutely filthy work around.... it seems the data does actually populate the graph is you just bypass the error message, and then set the y-axis scale to auto. See below for the code:

Option Explicit

Sub FirstChart()
    Dim FirstChartName As String
    Dim FirstChartRange As String
    Dim rng As Range
    Dim r As Range
    Dim shtData As Excel.Worksheet
    Dim shtDashboard As Excel.Worksheet
    Dim chart As Excel.chart
    Dim tmp As Variant

    Set shtData = ThisWorkbook.Sheets("Data")
    Set shtDashboard = ThisWorkbook.Sheets("Dashboard")
    
    ' get chart name
    FirstChartName = shtData.Range("B4").Value2
    ' get chart range
    FirstChartRange = shtData.Range("C4").Value2
    
    ' change data for first chart
    Set chart = shtDashboard.ChartObjects("FirstChart").chart
    With chart
        .HasTitle = True
        .ChartTitle.Text = FirstChartName
        On Error Resume Next
        .SetSourceData shtData.Range(FirstChartRange)
        On Error GoTo 0
        .Axes(xlValue).MaximumScaleIsAuto = True
    End With
       
End Sub

Hope this helps with your issue!

This is an example for creating a scatter plot. It should get you going. Adapt it to your needs.

Sub CreateChart()
    Dim wscharts As Worksheet, wsdata As Worksheet
    Set wscharts = Worksheets("Dashboard")
    Set wsdata = Worksheets("Data")
    Dim sh As Shape
    Set sh = wscharts.Shapes.AddChart2(240, xlXYScatterLines)
    sh.Select
    Dim rngText As String
    rngText = wsdata.Name & "!" & wsdata.Range("Data!$C$4").Value
    ActiveChart.SetSourceData Source:=Range(rngText)
    sh.Name = wsdata.Range("Data!$B$4").Value
End Sub

It works fine with the data as shown

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM