Pulling Trendline DataLabels to String Sporadically Working

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
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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