Getting Chart DataRange

AccessShell

Board Regular
Joined
Sep 5, 2015
Messages
50
Office Version
  1. 2003 or older
Platform
  1. Windows
I created a line chart based on data from another tab in the spreadsheet. I ran 'Record New Macro' from the tools tab tp get code to expand the DataRange.
That was fairly simple.
But, what I need is to get the value of the DataRange already in the chart. I want to change the datarange from code. The last point on the graph is usually well before the datarange for the chart.
I need to compare the datarange in the chart with the last point in the chart to decide if I need to (in code) expand the datarange.

I can easily convert Excel VBA to VB6

I hope this is clear.

NOTE: This was posted, with different wording, in another forum.

Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
There is no direct way to get the Datarange. If your data is in contigous range then it will be easy. Otherwise it will be slightly complex to retrieve what you want. The logic is to get the top and last cell from the 1st and last series and then construct the range. Let me see if I can quickly cough up a code.. gimme few minutes.
 
Upvote 0
See if this helps? I have not extensively tested it so please test it thouroughly before using in yuor vb6 application.

Sheet1 (Where the chart is)

1652133698421.png


Sheet2 (Where the data is)

1652133716294.png


Code

VBA Code:
Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim Chrt As Chart
    
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
    '~~> Change Chart 1 to the relevant chart name
    Set Chrt = ws.ChartObjects("Chart 1").Chart
    
    '~~> Find the first cell
    Dim FirstCell As Range
    Set FirstCell = Range(Split(Chrt.SeriesCollection(1).Formula, ",")(2))(1)
    
    '~~> Identify total series in the chart
    Dim SeriesCount As Long
    SeriesCount = Chrt.SeriesCollection.Count
    
    '~~> Find last cell
    Dim LastCell As Range
    With Range(Split(Chrt.SeriesCollection(SeriesCount).Formula, ",")(2))
        Set LastCell = .Item(.Count)
    End With
    
    '~~> Construct your data range
    Debug.Print Range(FirstCell.Offset(-1, -1), LastCell).Address(External:=True)
End Sub

Output

1652133873960.png
 
Upvote 0
Here, try this:

VBA Code:
Function GetChartDataRange(MyChart As Chart) As Range
  With MyChart
    Dim srs As Series
    For Each srs In ActiveChart.FullSeriesCollection
      Dim sFmla As String
      sFmla = srs.Formula
      sFmla = Mid$(Left$(sFmla, Len(sFmla) - 1), InStr(sFmla, "(") + 1)
      Dim vFmla As Variant
      vFmla = Split(sFmla, ",")
      Dim iFmla As Long
      For iFmla = 0 To 2
        Dim rFmla As Range
        On Error Resume Next
        Set rFmla = Range(vFmla(iFmla))
        On Error GoTo 0
        Dim DataRange As Range
        Set DataRange = JoinRanges(DataRange, rFmla)
        Set rFmla = Nothing
      Next
    Next
  End With
  Set GetChartDataRange = Intersect(DataRange.EntireColumn, DataRange.EntireRow)
End Function

Function JoinRanges(BaseRange, AddRange) As Range
  If BaseRange Is Nothing Then
    If AddRange Is Nothing Then
      ' nothing
    Else
      Set JoinRanges = AddRange
    End If
  Else
    If AddRange Is Nothing Then
      Set JoinRanges = BaseRange
    Else
      Set JoinRanges = Union(BaseRange, AddRange)
    End If
  End If
End Function
 
Upvote 0
Solution
SR. I cannot get past the statement
VBA Code:
Set ws = Sheet1
For my case I changed Sheet1 to the name of the sheet that has the charts. In my case "Charts ==>
VBA Code:
Set ws = Charts
I got an error - Type mismatch Run time error '13'

JP. I cannot get past the statement
VBA Code:
For Each srs In ActiveChart.FullSeriesCollection
FullSeriesCollection lights up. I got an error - Compile error, Method or data not found. I think there is relation to what I am passing to GetChartDataRange. I tried "Chart 1", it is chart #1 on the Charts sheet
 
Upvote 0
SR. I cannot get past the statement
VBA Code:
Set ws = Sheet1
For my case I changed Sheet1 to the name of the sheet that has the charts. In my case "Charts ==>
VBA Code:
Set ws = Charts
I got an error - Type mismatch Run time error '13'

JP. I cannot get past the statement
VBA Code:
For Each srs In ActiveChart.FullSeriesCollection
FullSeriesCollection lights up. I got an error - Compile error, Method or data not found. I think there is relation to what I am passing to GetChartDataRange. I tried "Chart 1", it is chart #1 on the Charts sheet
If your version if Excel is old, use .SeriesCollection instead of .FullSeriesCollection

Also you have to pass in a Chart, like this:

Excel Formula:
Dim ChartDataRange As Range
Set ChartDataRange = GetChartDataRange(ActiveChart)

Or

VBA Code:
Dim ChartDataRange As Range
Set ChartDataRange = GetChartDataRange(ActiveSheet.ChartObjects("Chart 1").Chart)
 
Last edited:
Upvote 0
JP. I get s type mismatch with your VBA code. It is on the Set statement
 
Upvote 0
JP. I can now get your code to work, sort of. I have no idea what Function JoinRanges(BaseRange, AddRange) As Range does.
When I run the code, the statement
VBA Code:
sFmla = srs.Formula
gives me
sFmla = =SERIES(,Activity!$A$5:$A$1102,Activity!$L$5:$L$1102,1)

The number 1102 is correct. It is what I am looking for, This chart has only one series (line). Does that mean I don't need the "for Eash srs..."
When I put my curser on the last data point in the chart, I get at the top of the sheet in the Fx area the same data that you gave me in sFmla.

When I changed the range manually, with Record New Macro, I went to the Source Data tab, the DataRange tab and changes the 1102 there.
I thought that's what I would do with code as a starting point. I think, if I use your sFmla with the new 1102, based on my calculations, I should be able to enter this new formula somehow.

Ihope I am not too confusing. Your code is great. Thanks
 
Upvote 0

Forum statistics

Threads
1,216,893
Messages
6,133,324
Members
449,798
Latest member
Jpull

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