bsanchezgt
New Member
- Joined
- Feb 20, 2018
- Messages
- 1
I have a scatter plot with 2 data sets plotted which can be updated from a userform. On the form in 2 labels, I update the caption to a projected value from the trend-line equation. When I step through this part of the code, I can see the trend-line equation from the object being pulled correctly, however, when just running it and using the userform, I get varied results. Sometimes, the first equation works out, sometimes both, and sometimes neither. The error that pops up is a type mismatch usually because the "Strend" string is empty, I think.
Am I using the DataLabel to string operation improperly? Or is there a more stable way to do this that will be more consistent? I have tried adding Chart.Refresh calls and activate and reactivates to solve to no avail.
PrivateSub Code:
Private Sub ProjCycle_Change()
Dim i As Integer
Dim trend As Trendline
Dim Strend As String
If ProjCycle.Value = "" Then
For i = 1 To 2
Fatigue.Controls("Log" & i).Caption = "N/A"
Next i
Else
'Trend Calculations
Application.ScreenUpdating = False
Application.EnableEvents = False
For i = 1 To 2
Worksheets("Fatigue").ChartObjects("Fatigue_Plot").Activate
' Get the trend line object
Set trend = ActiveChart.SeriesCollection(i).Trendlines(1)
' make sure equation is displayed
trend.DisplayRSquared = False
trend.DisplayEquation = True
' set number format to ensure accuracy
trend.DataLabel.NumberFormat = "0.00000000000000000000"
Worksheets("Fatigue").ChartObjects("Fatigue_Plot").Chart.Refresh
' get the equation
Strend = vbNullString
Strend = trend.DataLabel.Text
' massage the equation string into form that will evaluate
Strend = Replace(Strend, "y = ", "")
Strend = Replace(Strend, "ln", "*ln")
Strend = Replace(Strend, "x", ProjCycle.Value)
' evaluate for given x value
Fatigue.Controls("Log" & i).Caption = Format(Evaluate(Strend), "0.00")
' make sure equation is displayed
trend.DisplayRSquared = False
trend.DisplayEquation = False
Next i
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub
Am I using the DataLabel to string operation improperly? Or is there a more stable way to do this that will be more consistent? I have tried adding Chart.Refresh calls and activate and reactivates to solve to no avail.
PrivateSub Code:
Private Sub ProjCycle_Change()
Dim i As Integer
Dim trend As Trendline
Dim Strend As String
If ProjCycle.Value = "" Then
For i = 1 To 2
Fatigue.Controls("Log" & i).Caption = "N/A"
Next i
Else
'Trend Calculations
Application.ScreenUpdating = False
Application.EnableEvents = False
For i = 1 To 2
Worksheets("Fatigue").ChartObjects("Fatigue_Plot").Activate
' Get the trend line object
Set trend = ActiveChart.SeriesCollection(i).Trendlines(1)
' make sure equation is displayed
trend.DisplayRSquared = False
trend.DisplayEquation = True
' set number format to ensure accuracy
trend.DataLabel.NumberFormat = "0.00000000000000000000"
Worksheets("Fatigue").ChartObjects("Fatigue_Plot").Chart.Refresh
' get the equation
Strend = vbNullString
Strend = trend.DataLabel.Text
' massage the equation string into form that will evaluate
Strend = Replace(Strend, "y = ", "")
Strend = Replace(Strend, "ln", "*ln")
Strend = Replace(Strend, "x", ProjCycle.Value)
' evaluate for given x value
Fatigue.Controls("Log" & i).Caption = Format(Evaluate(Strend), "0.00")
' make sure equation is displayed
trend.DisplayRSquared = False
trend.DisplayEquation = False
Next i
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub