I have included the code of my function. I mostly scrapped this together from things I found online, because I am very much an amateur coder. I am trying to take the trendline of a graph and use it for a mathematical calculation. When I step through this code, it works great. However, when I call the function from another sub, it gives me an error. Error 9: Subscript out of range. When I debug, it shows me the line a = spl(0). The real problem is that the variable "s" remains empty. Why?
I have tried adding the creation of the chart to the function to avoid an error with "Active Sheet". I also tried pasting this code into my sub instead of calling a separate function. Still nothing. When I debug and highlight the t.DataLabel.Text, it shows me the correct value, but for some reason s is not saving that value. In the Locals window, t has value, but s is blank (" ").
Function TrendLineLog() As Double
Dim ch As Chart
Dim t As Trendline
Dim s As String
Dim Value As Double
' Get the trend line object
Set ch = ActiveSheet.ChartObjects(1).Chart
Set t = ch.SeriesCollection(1).Trendlines(1)
' make sure equation is displayed
t.DisplayRSquared = False
t.DisplayEquation = True
' set number format to ensure accuracy
t.DataLabel.NumberFormat = "0.000000E+00"
' get the equation
s = t.DataLabel.Text '<--------- HERE
' massage the equation string into form that will evaluate
s = Replace(s, "y = ", "")
s = Replace(s, "ln", " *LOG")
s = Replace(s, " +", "")
s = Replace(s, " - ", " -")
spl = Split(s, " ")
a = spl(0) '<----------- HERE
b = spl(1)
c = spl(2)
y = 0.5
..... Math stuff
End Function
Here is the code section calling the function:
For rowx = 41 To 46 Step 1
Cells(rowx, 4).Select
va = Cells(rowx, 4).Value
vb = Cells(rowx, 5).Value
vc = Cells(rowx, 6).Value
vd = Cells(rowx, 7).Value
locb = ActiveCell.Address
Cells(rowx, 7).Select
loce = ActiveCell.Address
rang = locb & ":" & loce
If va < 0.8 Then
If va < vb And vb < vc And vc < vd Then
Range(rang).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range(rang)
ActiveChart.FullSeriesCollection(1).Trendlines.Add
ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
Selection.DisplayEquation = True
Selection.Type = xlLinear
ans = TrendLineLin()
Sheets("Results").Activate
Cells(rowx - 39, 3).Value = ans
Sheets(nam).Activate
ElseIf va < vb And vb < vc And vc > vd Then
Range(rang).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range(rang)
ActiveChart.FullSeriesCollection(1).Trendlines.Add
ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
Selection.DisplayEquation = True
Selection.Type = xlLogarithmic
ans = TrendLineLog()
Sheets("Results").Activate
Cells(rowx - 39, 3).Value = ans
Sheets(nam).Activate
ElseIf va < vb And vb < vc And vc > vd Then
Range(rang).Select
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
ActiveChart.SetSourceData Source:=Range(rang)
ActiveChart.FullSeriesCollection(1).Trendlines.Add
ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
Selection.DisplayEquation = True
Selection.Type = xlLogarithmic
Windows(nam1).Activate
ans = TrendLineLog(rang)
Windows(nam1).Activate
Sheets("Results").Activate
Cells(rowx - 39, 3).Value = ans
Sheets(nam).Activate
I have a function for exponential, logarithmic, and linear trendlines. My test case is a log, which is the function that is posted. The code is nearly identical.