Modifying chart trendline without activating sheet

MrPez

Board Regular
Joined
Jan 28, 2010
Messages
128
I have some code that updates trendlines on a chart. The code runs fine if the sheet with the chart is active, but not if another sheet is active (which will be the case).

I get Type Mismatch on the highlighted line. Any ideas?
Code:
Sub Trendline_Update()Dim PVTrend As String
Dim EITrend As String
Dim PVType As String
Dim EIType As String
Dim STPChart As Chart
Dim DetNo As Worksheet
'Updates trendlines in DR report according to the PV/EI-dose relationship determined by the DRAnalysis code


For i = 1 To 2


    If i = 1 Then Set DetNo = Worksheets("Detector1_Report")
    If i = 2 Then Set DetNo = Worksheets("Detector2_Report")


    Set STPChart = DetNo.ChartObjects("Chart 1").Chart
    
    PVTrend = Range("M30")
    EITrend = Range("M31")
    
    Select Case PVTrend
        Case Is = "Linear"
            PVType = xlLinear
        Case Is = "Polynomial (2nd order)"
            PVType = xlPolynomial
        Case Is = "Power"
            PVType = xlPower
        Case Is = "Logarithmic"
            PVType = xlLogarithmic
        Case Is = "Exponential"
            PVType = xlExponential
    End Select
    
    Select Case EITrend
        Case Is = "Linear"
            EIType = xlLinear
        Case Is = "Polynomial (2nd order)"
            EIType = xlPolynomial
        Case Is = "Power"
            EIType = xlPower
        Case Is = "Logarithmic"
            EIType = xlLogarithmic
        Case Is = "Exponential"
            EIType = xlExponential
    End Select
    
    With STPChart
[COLOR=#ff0000]        .SeriesCollection("PV").Trendlines(1).Type = PVType[/COLOR]
        .SeriesCollection("PV").Trendlines(1).DataLabel.Font.ColorIndex = 49
        .SeriesCollection("EI").Trendlines(1).Type = EIType
        .SeriesCollection("EI").Trendlines(1).DataLabel.Font.ColorIndex = 9
    End With
    
Next i


End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Slightly narrowing the problem down:

Getting
Code:
print stpchart.Name
shows that STPChart is named as
"[ActivesheetName] Chart 1"
instead of
"Detector1_Report Chart 1"

I don't know what this is!
 
Upvote 0
nevermind, was me being daft.
Should have been
Code:
[COLOR=#333333]PVTrend = DetNo.Range("M30")[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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