RockandGrohl
Well-known Member
- Joined
- Aug 1, 2018
- Messages
- 801
- Office Version
- 365
- Platform
- Windows
Hi guys, have done way too much searching on this, even wrestling with ChatGPT which is spinning me round in circles.
I have a chart called "CurveChart", and when I select information from 2 drop down boxes it populates a simple scatter chart with the data points, and a linear trendline, which then has the trendline Equation displayed as a data label.
What I'm trying to do is to get that Trendline equation to be input into cell H3 on the same sheet.
Below is my code. I am getting "parameter not valid"
My chart only has one series, and one trendline. When the drop-down boxes are filled in, it filters a table on Database (db) tab which then populates the series in the chart.
The part of the code that's erroring out is the strFormula lines near the bottom - neither of these seem to work. Whenever one works, I would simply drop that value into cell H3.
Cheers!
I have a chart called "CurveChart", and when I select information from 2 drop down boxes it populates a simple scatter chart with the data points, and a linear trendline, which then has the trendline Equation displayed as a data label.
What I'm trying to do is to get that Trendline equation to be input into cell H3 on the same sheet.
Below is my code. I am getting "parameter not valid"
My chart only has one series, and one trendline. When the drop-down boxes are filled in, it filters a table on Database (db) tab which then populates the series in the chart.
VBA Code:
Private Sub worksheet_change(ByVal Target As Range)
Dim Keycells As Range
Dim cur As Worksheet, calc As Worksheet, db As Worksheet
Dim strFormula As String, asset As String, inter As String
Dim Lastrow As Long
Dim chart As ChartObject
Dim series As series
Dim trendline As trendline
Set Keycells = Range("B3:H5")
Set cur = Worksheets("Curves")
Set db = Worksheets("Database")
Set calc = Worksheets("Calculations")
Application.ScreenUpdating = False
If Not Intersect(Range("B3"), Target) Is Nothing Then
Application.EnableEvents = False
If Range("B3").Value = "" Then
On Error Resume Next
db.ShowAllData
On Error GoTo 0
cur.Range("B5").Value = ""
Else
If Not Range("B5").Value = "" Then
Range("B5").Value = ""
End If
On Error Resume Next
db.ShowAllData
On Error GoTo 0
calc.Range("M2:N500").ClearContents
calc.Range("I2:I500").ClearContents
asset = Range("B3").Value
' Disable line on chart
Lastrow = db.Cells(Rows.Count, "A").End(xlUp).Row
db.Range("H1").AutoFilter 8, asset
calc.Range("H2").Value = asset
'Add count in here to check visible rows exist
db.Range("K2:K" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
calc.Range("I2").PasteSpecial (xlPasteValues)
cur.ChartObjects("CurveChart").Activate
ActiveChart.ChartType = xlXYScatter
End If
End If
Application.EnableEvents = True
If Not Intersect(Range("B5"), Target) Is Nothing Then
inter = Range("B5").Value
calc.Range("M2:N500").ClearContents
On Error Resume Next
db.ShowAllData
On Error GoTo 0
Lastrow = db.Cells(Rows.Count, "A").End(xlUp).Row
asset = cur.Range("B3").Value
db.Range("A1:AG" & Lastrow).AutoFilter 8, asset
db.Range("A1:AG" & Lastrow).AutoFilter 11, inter
db.Range("A1:AG" & Lastrow).AutoFilter 20, "<>"
'db.Range("T2:T" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
'calc.Range("M2").PasteSpecial (xlPasteValues)
'db.Range("P2:P" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
'calc.Range("N2").PasteSpecial (xlPasteValues)
cur.ChartObjects("CurveChart").Activate
ActiveChart.ChartType = xlXYScatter
'strFormula = ActiveSheet.ChartObjects("CurveChart").SeriesCollection(1).Trendlines(1).DataLabel.Text
'strFormula = ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Text
Range("H3").Value = strFormula
Else
cur.ChartObjects("CurveChart").Activate
ActiveChart.ChartType = xlXYScatter
End If
Application.ScreenUpdating = True
End Sub
The part of the code that's erroring out is the strFormula lines near the bottom - neither of these seem to work. Whenever one works, I would simply drop that value into cell H3.
Cheers!