Auto update to a chart

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a chart on a page and the data set will change based on what the user selects and transfers via another macro I have. I have a chart on the page that displays the data. I have done some digging and added a macro to update the charts display. I want the presented chart data to fill in the size of the chart window on the page. I have included an example of my chart and macro code below. My code crashes on the line below:
'Sets new range for each series
ActiveChart.SeriesCollection(n).Values = rng

I get a Run-Time Error '1004' Application defined or object-defined error. I don't know how to get it to work and auto adjust the chart.


My code:

VBA Code:
Sub Change_Chart_Range()

    Dim i As Integer
    Dim r As Integer
    Dim n As Integer
    Dim p1 As Integer
    Dim p2 As Integer
    Dim p3 As Integer
    Dim rng As Range
    Dim ax As Range
    Dim ch As ChartObject
    Dim ws As Worksheet
   
    Set ws = ActiveSheet
   
    ws.ChartObjects.Select
   
    'Cycles through each series
    For n = 1 To ActiveChart.SeriesCollection.Count Step 1
        r = 0

        'Finds the current range of the series and the axis
        For i = 1 To Len(ActiveChart.SeriesCollection(n).Formula) Step 1
            If Mid(ActiveChart.SeriesCollection(n).Formula, i, 1) = "," Then
                r = r + 1
                If r = 1 Then p1 = i + 1
                If r = 2 Then p2 = i
                If r = 3 Then p3 = i
            End If
        Next i


        'Defines new range
        Set rng = Range(Mid(ActiveChart.SeriesCollection(n).Formula, p2 + 1, p3 - p2 - 1))
        Set rng = Range(rng, rng.Offset(0, 1))

        'Sets new range for each series
        ActiveChart.SeriesCollection(n).Values = rng
     
   
        'Updates axis
        Set ax = Range(Mid(ActiveChart.SeriesCollection(n).Formula, p1, p2 - p1))
        Set ax = Range(ax, ax.Offset(0, 1))
        ActiveChart.SeriesCollection(n).XValues = ax

    Next n
End Sub

Chart and dara range:

Machine Follow-Up Test.xlsm
ABCDEFGHIJKLMNOPQRST
1
2JOB #Start DateEnd DateDelayDurationData Set:Test 1
3AAA18-Jan-202022-Jan-20204
4BBB11-May-202015-May-20204
5CCC23-Jun-202025-Jun-20202
6DDD15-Jul-202017-Jul-20202
7EEE3-Aug-20205-Aug-20202
8FFF17-Aug-202021-Aug-20204
9GGG9-Sep-202011-Sep-20202
10HHH21-Oct-202023-Oct-20202
11III7-Dec-202011-Dec-20204
12JJJ4-Jan-20218-Jan-20214
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
32
33
Combo Gantt Chart
Cell Formulas
RangeFormula
E3:E31E3=DATEDIF(B3,C3,"d")+D3



Unfortunately my chart did not come through on the xl2bb transfer, so I have pasted it below.

Chart_1.PNG


Thanks for the help.
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
That line fails because you are assigning a 2xN array to the Values (and XValues a bit farther down) when you should be assigning a 1xN vector.

What version of Excel are using? I am not familiar with an Excel 'Combo Gantt Chart'.
 
Upvote 0
I am using Excel 365 and Excel Combo Gantt Chart is just my name for the tab and what I am trying to accomplish. I have figured out how to modify the X axis to fit the max and min values, but I still do not know how to adjust the Y axis so that only the rows with data in them will be plotted on the chart.
 
Upvote 0
Please post a few of the series formulas for your chart.

You could delete all of the series on the current chart then modify this to add the series for each populated row:

VBA Code:
Sub CreateGraphInVBA()
    'Much taken from http://peltiertech.com/Excel/Charts/InteractiveChartCreation.html
    
    Dim rOEERange As Range
    Dim rDateRange As Range
    Dim objChart As ChartObject
    
    Set rOEERange = ActiveSheet.Range("B1:B20")
    Set rDateRange = ActiveSheet.Range("A1:A20")
    
    Set objChart = ActiveSheet.ChartObjects(1)
    With objChart.Chart
        .SeriesCollection.Add (ActiveSheet.Range("A1"))
        .SeriesCollection(1).XValues = rDateRange
        .SeriesCollection(1).Values = rOEERange
        .PlotBy = xlColumns
        .ChartArea.AutoScaleFont = False
        .ChartType = xlBarStacked
        .HasTitle = True
        .ChartTitle.Characters.Text = "My Title"
        .ChartTitle.Font.Bold = True
        .ChartTitle.Font.Size = 12
    End With
    
    Set rOEERange = Nothing
    Set rDateRange = Nothing
    Set objChart = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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