+ Reply to Thread
Results 1 to 8 of 8

Macro to calculate slopes of multiple lines and place values into a column

  1. #1
    Registered User
    Join Date
    06-06-2017
    Location
    CHICAGO
    MS-Off Ver
    2016
    Posts
    7

    Macro to calculate slopes of multiple lines and place values into a column

    Poweder Dryer.xlsx

    Need help creating a Macro that will calculate the slopes (flow rates) of multiple lines and place them in a column. On each of 8 worksheets in one workbook, I have one X data column and forty Y data columns, therefore I need forty slopes for each worksheet. So far I have been creating a graph and manually inputting the slopes into a column but need to automate this process. Please see attached images. For some reason, I am unable to attach an excel file. Your help would be much appreciated.
    Attached Images Attached Images
    Last edited by chimukanm2; 06-06-2017 at 12:30 PM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,958

    Re: Macro to calculate slopes of multiple lines and place values into a column

    Take a look at the LINEST function.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    06-06-2017
    Location
    CHICAGO
    MS-Off Ver
    2016
    Posts
    7

    Re: Macro to calculate slopes of multiple lines and place values into a column

    Quote Originally Posted by dflak View Post
    Take a look at the LINEST function.
    Thank you for your response. I am new to this forum and figured out how to add an attachment. Please see attached excel file named "Powder Dryer" in original post.

  4. #4
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,958

    Re: Macro to calculate slopes of multiple lines and place values into a column

    What rows / columns contain your Y-values, what rows / columns contain your X-values?

  5. #5
    Registered User
    Join Date
    06-06-2017
    Location
    CHICAGO
    MS-Off Ver
    2016
    Posts
    7

    Re: Macro to calculate slopes of multiple lines and place values into a column

    The X's are in the Time (s) column. The Y's are Batch 1 through 40.
    the rows start from the first top value until the first 0 value in each column.
    Last edited by chimukanm2; 06-06-2017 at 01:07 PM.

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,958

    Re: Macro to calculate slopes of multiple lines and place values into a column

    The formulas you want are in column BA.

    I suggest you read up on the LINEST function; it appears that it can be a big help to you in the line of work you are in. LINEST doesn’t only calculate slope and intercept, but also other metrics associated with the correlation of the points to the equation. It can also be set up to produce the coefficients for higher order polynomials. Do a web search on LINEST and polynomial .

    LINEST provides a higher degree of accuracy for coefficients than is provided by the trend line formula provided by the chart. So my numbers are slightly different than yours.

    In its simplest form LINEST is =LINEST(y-values, x-values). Y and X must have the same number of data points. LINEST is an array formula so you have to enter it with CTRL-SHIFT-ENTER. Normally you would select the an array of cells and LINEST will fill in the array with the coefficients and other metrics.

    Since all you want is slope and slope happens to be the first element of the array, I could use a one by one array: in other words a single cell. I still had to enter it as an array formula.

    There is more to this task than just providing the X’s and Y’s. I am assuming that once a value reaches zero, you don’t want to include those other zeros. So we just can’t use rows 4 to 54. We only want to use rows that are non-zero.

    Fortunately your data are contiguous descending – there are no “embedded: zeros. So COUNTIF(Range,”>0”) will give me the number of rows I need.

    To define the ranges to use in the LINEST command we need to use the OFFSET command.

    The offset command for the X-values is: OFFSET($B$4,0,0,COUNTIF($C$4:$C$54,">0"),1)

    Here is a blurb on how offset works: http://www.utteraccess.com/wiki/Offs...Dynamic_Ranges

    OFFSET takes 5 parameters.
    - A start cell
    - Number of cells to go down
    - Number of cells to go right
    - Number of rows to return
    - Number of columns to return

    So in the above command. We start in cell B4, go down zero rows and over zero columns, so we are still at cell B4. From that point we want to look at a range COUNTIF(C4:C54) rows deep and one column wide.

    In the case of column C, we only want to look at 35 rows. Other columns compute to different values.

    A similar calculation is made for the number of Y’s to look at.

    You will note that for every column, the number of X’s to look at depends on the number of non-zero Y’s to look at.

    Because the data are and desired results are transposed – the batch data is across the columns and the batch results are down the rows – you just can’t copy and paste the formulas and expect relative addressing to take care of pointing to the correct cells.

    I did the first couple of rows in column BA. You need to copy the formula in cell B4 all the way down (Copy and paste, do not drag – you can’t drag array formulas). Then you have to go in cell by cell and change all references to $C in the formula to the column letter associated with the batch. Once you make the correction, confirm the formula with CTRL-SHIFT-ENTER.

    LINEST will do the work for you. You don't need VBA for this.

    Good luck with your project.
    Attached Files Attached Files

  7. #7
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,958

    Re: Macro to calculate slopes of multiple lines and place values into a column

    I just noticed something in the wording of your request. COUNTIF($C$4:$C$54,">0") includes all non-zeros. If you want it to include the first zero, change it wherever you see it to COUNTIF($C$4:$C$54,">0") + 1

  8. #8
    Registered User
    Join Date
    06-06-2017
    Location
    CHICAGO
    MS-Off Ver
    2016
    Posts
    7

    Re: Macro to calculate slopes of multiple lines and place values into a column

    Thank you so much for your help @dflak. This technique is very helpful in calculating the slope and has taken me a step further into completing my project.
    To take it a step further, I would like a situation where I could input my batch data, hit a calculate button and have the slope column automatically populate.
    Can I achieve this through the use of a macro?
    Last edited by chimukanm2; 06-07-2017 at 09:07 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculate different where there are multiple values in column fields
    By stephme55 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 04-07-2016, 03:13 PM
  2. Automate calculation of multiple slopes from data set
    By gmpurdy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-10-2016, 12:12 AM
  3. [SOLVED] Need Macro to Distribute Values within a Cell to Multiple Lines
    By Ocean Zhang in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2014, 02:34 AM
  4. Macro to calculate dynamic ranges for multiple values from a second column
    By sykadelikfur in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2013, 01:55 PM
  5. Replies: 6
    Last Post: 07-12-2013, 06:32 AM
  6. macro for printing only lines with values in a specific column
    By taffocon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-12-2013, 08:08 AM
  7. Macro to transpose one column into multiple rows over MANY lines of data?
    By mtc in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-24-2012, 10:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1