Share via

Adding Tredline to Chart using VBA - Getr type Mismatch

Anonymous
2015-08-07T15:02:15+00:00

Hi,

I am using the below piece of code to add a trendline to a graph.  But it fails with runtime error 13 - type mismatch

Sub AddTrendline()

    Dim GraphName As String

    Dim TType As String

    GraphName = Sheets("Control").Cells(11, 2).Value

    TType = "xl" & Sheets("Control").Cells(9, 2).Value  ' Tredline Type - Contains Linear

    PO = Sheets("Control").Cells(10, 2).Value           ' Trendline Parameter

    Call UPS(Sheets("Control").Cells(11, 2).Value, "6785")

    Sheets(GraphName).Select

    ActiveChart.PlotArea.Select

    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=TType).Select

    Call PS(Sheets("Control").Cells(11, 2).Value, "6785")

End Sub

The varialble TType is instantiated correctly.

 If I change the code to the below it works. I put the trendline type explicitly.

ActiveChart.SeriesCollection(1).Trendlines.Add(Type**:="xlLinear").**Select

Sub AddTrendline()

    Dim GraphName As String

    Dim TType As String

    GraphName = Sheets("Control").Cells(11, 2).Value

    TType = "xl" & Sheets("Control").Cells(9, 2).Value  ' Tredline Type - Contains Linear

    PO = Sheets("Control").Cells(10, 2).Value           ' Trendline Parameter

    Call UPS(Sheets("Control").Cells(11, 2).Value, "6785")

    Sheets(GraphName).Select

    ActiveChart.PlotArea.Select

    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:="xlLinear").Select

    Call PS(Sheets("Control").Cells(11, 2).Value, "6785")

End Sub

How can I pass in a variable to set the trendline.  The same would apply if I user Power or Polynomial types as there us a second parameter.

Reagrds

Tim

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2015-08-07T16:03:55+00:00

    If PO is your variable for polynomial order, you can do something like this:

        If TType = xlPolynomial Then

            PO = Sheets("Control").Cells(10, 2).Value

        Else

            PO = Null

        End If

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-08-07T15:43:47+00:00

    Hans,

    Thank you that worked a dream.  I will now work on setting the other options based on Trendline type, for example .  if the Trendline Type is xlPolynomial, I want to set the trendline order so will need a second parameter to be set, Variable type from doc must be Variant, but contain and an integer from 2 to 6 inclusive. Kind regards,Tim

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2015-08-07T15:20:37+00:00

    The Trendline Type is an integer, not a text string.

    If you specify the type as a literal string "xlLinear", Excel is smart enough to translate it to the corresponding integer, but that doesn't work if you use a string variable.

    Try this:

    Sub AddTrendline()

        Dim GraphName As String

        Dim TType As XlTrendlineType

        'Dim PO

        GraphName = Sheets("Control").Cells(11, 2).Value

        Select Case Sheets("Control").Cells(9, 2).Value

            Case "Linear"

                TType = xlLinear

            Case "Polynomial"

                TType = xlPolynomial

            Case "Exponential"

                TType = xlExponential

            Case "Logarithmic"

                TType = xlLogarithmic

            Case "Power"

                TType = xlPower

            Case "MovingAvg"

                TType = xlMovingAvg

        End Select

        ' PO doesn't get used...

        'PO = Sheets("Control").Cells(10, 2).Value ' Trendline Parameter

        Call UPS(GraphName, "6785")

        Sheets(GraphName).Select

        ActiveChart.PlotArea.Select

        ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=TType).Select

        Call PS(GraphName, "6785")

    End Sub

    Was this answer helpful?

    0 comments No comments