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?
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