Extracting trendline slope with VBA - excel

I'm currently making a macro to extract raw data from multiple csvs, print it in different sheets, trim extraneous data, plot data from each sheet on a single graph with individual trendlines, and report some information for said trendlines and data. I've had success with everything except extracting the trendline slopes. I want to report the slope average of all the trendlines and I've written the code below to do this action:
For j = 1 To UBound(SelectedSheets)
TLEq = CompositeChartObject.Chart.SeriesCollection(j).Trendlines(1).DataLabel.Text
ReDim Preserve ModulusArr(j)
ModulusArr(j) = CDbl(Mid(TLEq, 4, Len(TLEq) - 4))
Next j
SelectedSheets is an array filled with the sheets the user selects to be analyzed from a multi-select userform listbox. TLEq is a string to temporarily hold the full text of the trendline equation (intercept set to 0 so simple y = mx equation). The CDbl(Mid()) is used to trim TLEq to just the slope and convert to a Double. In lines above these (not included), the series are added, trendlines inserted, and DisplayEquation is called for each trendline. There has been no issues with graphing, trendlines showing up, or trendline equations displaying.
The issue begins in line 2 where TLEq is inconsistently left blank during the loop; some iterations all the trendline equation are extracted, sometimes only some are, and other times no equations are extracted.. I have yet to find any pattern. Then, anytime TLEq is empty, line 4 errors because it cannot carry out the CDbl(Mid()) an empty TLEq. Any ideas?

Related

Excel : dynamic data range for graph with multiple lines

To be simple, I have a Excel PowerQuery request which download a table from the web.
I'm trying to draw a graph with multiple lines, the horizontal axis is always the dates, and the vertical axis is composed from all the lines.
Here an example of what it look :
My objective is to catch all the data of the table dynamically without having every time to extend the data range by clicking on the graph.
On the data range :
By using =Table_0__6[[#Tout] I get all the table data, but my goal is to catch only the Dates column (horizontal axis) and two other columns of entry for the data series.
I tried many ways.
By example, if the columns were adjacent, the data range could be :
=Table_0__6[[#Tout];[Date]:[Column 2]]
Since the entry columns are not adjacents, I tried to link the data series by ; or + like this : (not working)
=Table_0__6[[#Tout];[Date]:[Column 1]]+Table_0__6[[#Tout];[Date]:[Column 2]]
or even : (not working)
=Table_0__6[[Date];[Column 1]+[Column 2]];
My question is : Do you have any idea on how catching in horizontal axis every data of Date column from the table, and for the veritcal axis every data of column 1 and 2 ?
Have a great end of week
Respectfully
Thanks

Need VBA code to plot up series of linear data in Excel, create regression eq's for each series, then write the regression eq's in the Excel sheet

I have a fairly simple VBA code which is doing 75% of what I need it to do, I just can't get the last step to work properly.
In an Excel sheet, I have two sets of data (X data and Y data). The x data is the same two data values for every single case. Then I have two columns of data which comprise the Y data. I need to create an Excel scatter plot with a whole bunch of data series. Series 1 is the two constant x values with the first y value from the first column and the first y value from the second column. The 2nd series is the same two constant x values with the second y value from the first column and the second y value from the second column, and so on. The columns have about 300+ rows (hence > 300 series total). I realize that a scatter plot can only do up to about 256 series in one plot so I won't be able to fit them all in one plot. That's not the point. I've tried just plotting like, say, 50 of the series and I have that part working fine. Next, because all the series are straight lines, I need the slope/regression information from each series, so I create a linear trendline and plot the equation on the chart. I have this part automated and working correctly in Excel as well. Here's where I'm stuck though. The last step is, after I've had VBA create the, say, 50 series in the single plot and generate all of the regression equations, I then need for Excel to take those equations and paste them into a column in the main worksheet (where the plotting data resides). I found something on Google that is supposed to do this (grabbing trendline.datalabel.text and pasting it into the sheet), but it isn't working right, and I can't figure out what I'm doing wrong. I've tried 20 different things and am still beating my head against a wall. Any help with the last half of the code would be greatly appreciated. THANK YOU.
Sub Plot_slopes()
Dim i As Integer
Dim ChtOb As ChartObject
Dim objTrendline As Trendline
Dim strEquation As String
Set ChtOb = ActiveSheet.ChartObjects.Add(Left:=20, Width:=800, Top:=20, Height:=250)
ChtOb.Chart.ChartType = xlXYScatterSmoothNoMarkers
ChtOb.Activate
i = 9
For i = 9 To 59
With ActiveChart.SeriesCollection.NewSeries
.Name = "FS" & i
.XValues = Worksheets("summary").Range(Worksheets("summary").Cells(3, 7), Worksheets("summary").Cells(4, 7))
.Values = Worksheets("summary").Range(Worksheets("summary").Cells(i, 13), Worksheets("summary").Cells(i, 14))
.Trendlines.Add
End With
With ActiveSheet.ChartObjects(1).Chart
Set objTrendline = .Trendlines(1)
With objTrendline
.DisplayRSquared = False
Trendlines(1).DisplayEquation = True
strEquation = .DataLabel.Text
Range("Q9").Offset(i, 0) = strEquation
End With
End With
Next i
End Sub
I mainly need help with the code after the first With Block and where it starts with a new "with"..."with activesheet.chartobjects(1).chart", etc), although, I'm also having trouble with the .name part in the first With block as well. Goal is to get the last block so that VBA writes out the regression equations for each created series into the main "summary" worksheet

Excel Equation of line not correct

I'm hoping someone will be able to tell me why the equation that Excel generated is not giving the correct results as it is graphed correctly.
I have some X and Y points that I will list below. I plotted those points in Excel and then plotted the trend line, and had it show me the equation of the trendline. When I take the equation and then plug in the X values I get very different answers back.
X and Y Values
X Y
0 3
3 2
5 1.4
7 1
10 0.5
18 0.1
When I set the intercept to 3, the equation of the trendline is y = 0.0088x5 - 0.1457x4 + 0.8753x3 - 2.224x2 + 1.4798x + 3
Screenshot of Excel window with equation
Any help is greatly appreciated.
I suspect you didn't set up your graph correctly.
Select a single cell in your table
Insert/Scatter (and decide which you want with regard to markers, etc)
Select the line and add Trendline
Set you parameters for the trendline
If you want to get the formula for the trendline from the "show formula" option, be sure to format the trendline label to be numeric with 15 decimals. Otherwise the equation will certainly not work, even if it appears to be correct.
Note that you can obtain the formula directly using the LINEST worksheet function.
=LINEST(Y,X^{1,2,3,4,5}) returns the array:
{0.0000399230399230442,-0.00152188552188569,0.0192991822991846,-0.0840134680134806,-0.217128427128402,2.99999999999999}
The last value in the array is the y-intercept
The slight differences are due to the use of different algorithms for the two methods.

charting trendlines together

I need to display this kind of data in one chart:
Which means:
Function A: Y= X + 10 ;
Function B: Y= X * 2 ;
but what I got from excel is:
Actually what I want is (sorry for the drawing):
My real data is like:
If I format it like this:
I can still not get the correct charts.
Updated to account for change of question
Your data structure is wrong to achieve this in Excel, see for example:
On the left you want your x axis values, don't put a header in this column otherwise it will plotted on your graph as a series (extra line).
You can fill in gaps in your data with #N/A and excel will ignore these points, joining the line to the next available point.

Excel - Line chart from date and time

How can I make line chart from this data?
Two lines
X values = time
Y valies = number
This is just small sample of data (I have more than 30 day).
(I use Excel 2016)
Thanks
Make sure your X-axis is the first column and your Y axis is your 2, 3, 4, etc column
Highlight you data that you want to work with, minimum two column.
Find your Insert Ribbon
Select Insert Chart about halfway across and and make sure you select the line one. (Personally I would use and XY scatter plot with lines)
In your case where you have X, Y , X, Y as the sequence of your column, I would edit your data series afterwards to eliminate the series which are really your second X values. Then go to your subsequent Y values, edit their series, and select a new X series for each.
Its usually much easier if you place all your X data in one column, and each Y series in its own column making sure it lines up with the appropriate X data.

Resources