excelvbaexcel-charts

Update chart range with dynamic range


I've been trying to figure this out for past 2 days but I just can't find a proper solution.

Here's my code:

Private Sub Update()

    Worksheets("PBF_Graphs").Activate
    
    ThisWorkbook.Worksheets("PBF_Graphs").Unprotect Password:="test"
    
    Dim lastRow As Range
    Dim modelSheet As String
    Dim currentModel As String
    Dim boltNo As Integer
    Dim boltRange As Range
    Dim BSN_Range As Range
    Dim chart As ChartObject
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim chartName As String
    Dim graphNo As Integer
    Dim chartNo As String
    Dim ch As String
    Dim i As Integer
    Dim j As Integer
    Dim currentFixing As String

    Set wb = ActiveWorkbook
    Set ws = wb.ActiveSheet
    Set lastRow = Range("C2")
    Set BSN_Range = Range("$C$2")
    chartNo = "Chart "
    
    Const fixing1 = "$G$1"
    Const fixing2 = "$I$1"
    Const fixing3 = "$K$1"
    Const fixing4 = "$M$1"
    
    currentFixing = "$G$1"
    
    ' set up bolt number as 1 for incrementation always start from FP-FB and finish a RP-RB
    boltNo = 1
    Set boltRange = Range("G2")
    
    
    currentModel = MainMenu.globalModel
    
    ' set current model name
    Select Case currentModel
    Case "Model1"
        modelSheet = "DP025"
        graphNo = 3
        j = 3
    Case "Model2"
        modelSheet = "DP025_2"
        graphNo = 7
        j = 7
    Case "Model3"
        modelSheet = "DP025_3"
        graphNo = 11
        j = 11
    Case "Model4"
        modelSheet = "DP025_4"
        graphNo = 16
        j = 16
    End Select
    
    For i = graphNo To graphNo + 3
    
    ' this will automatically change the chart
    ws.ChartObjects(chartNo & j).Activate
    ActiveChart.SeriesCollection(1).Select
    Application.CutCopyMode = False
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(1).Name = modelSheet & currentFixing
    ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & boltRange.End(xlDown) ' <- doesn't work gives 1004 error
    ' ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & "G2:G999" <- works but is not dynamic and if there are blanks it will show in chart
    ActiveChart.FullSeriesCollection(1).XValues = modelSheet & BSN_Range.End(xlDown) ' <- doesn't work either
    
    j = j + 1
    
    incrementRange boltNo, boltRange, currentFixing
    
    
    Next i
                         
        
    ThisWorkbook.Worksheets("PBF_Graphs").Protect Password:="test"

End Sub

Function incrementRange(ByVal bNumber As Integer, bRange As Range, cFixing As String)

    bNumber = bNumber + 1
    
    Select Case bNumber
    Case 1
        Set bRange = Range("$G$2")
        cFixing = "$G$1"
    Case 2
        Set bRange = Range("$I$2")
        cFixing = "$I$1"
    Case 3
        Set bRange = Range("$K$2")
        cFixing = "$K$1"
    Case 4
        Set bRange = Range("$M$2")
        cFixing = "$M$1"
    End Select

End Function

I commented the part that doesn't work as I want to and what works as I don't want to. Basically the data provided for these charts change, so the range is not fixed and I need to update dynamically based on sample size.

Edit:

The boltRange Range is not working, giving the 1004 application-defined or object-defined error, where as a static string of for example "G2:G999" works, I need that boltRange.End(xlDown) which I want to change according to the required column and go down to the last value


Solution

  • As stated in comments

    Changing the following in:

    ' this will automatically change the chart
        ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & boltRange.End(xlDown) ' <- doesn't work gives 1004 error
        ' ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & "G2:G999" <- works but is not dynamic and if there are blanks it will show in chart
        ActiveChart.FullSeriesCollection(1).XValues = modelSheet & BSN_Range.End(xlDown) ' <- doesn't work either
    

    To the following:

    ' this will automatically change the chart
        ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & boltRange.Address & ":" & _
            Worksheets(modelSheet).Cells(Rows.Count, boltRange.Column).End(xlUp).Address
        ActiveChart.FullSeriesCollection(1).XValues = modelSheet & "!" & BSN_Range.Address & ":" & _
            Worksheets(modelSheet).Cells(Rows.Count, BSN_Range.Column).End(xlUp).Address