Replicating Array Formula

jaustin

New Member
Joined
Jun 6, 2017
Messages
29
I recently stumbled across array formulas (specifically LINEST)which could be a big help if I can learn how to use it properly. While I can set-up a 2x5 cell region for theresults, I only need and slope and intercept. If I turn the Stat option to False this is exactly what I get and need(which are in cells 1 and 2 of row 1 of the result matrix). However, I want to apply this formula to atable in which the x values are in “DU2:EN2” and the y values are “DU3:EN3” butthe table goes from rows 3-83 (y values). Myquestion is how to apply this formula to each row (from 3 to 83) using VBA. I’ve tried various approaches but continue toget the remainder of the initial formula in rows 4-7. I would also like to make the solutionvariable so that y rows can be variable (except for row 3) as this table will have variable y rows.

I just purchased the MrExcel book on array formulas but haven't received it yet. Can't believe I hadn't known about this Excel feature before now.

Thanks in advance,

J. Austin



 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It depends on how you're trying to use the LINEST output, but I find it useful to leverage the INDEX function.

For example, if I type this formula:

Rich (BB code):
=LINEST(INDEX($DU$3:$EN$83, 1, 0), $DU$2:$EN$2)

I am indexing the matrix of all the possible y-value, and specifying the row. In the above example, I would get the output for DU3:EN3. If I change the 1 to 2:

Rich (BB code):
=LINEST(INDEX($DU$3:$EN$83, 2, 0), $DU$2:$EN$2)

that would give me the result for DU4:EN4 y-values. And so on all the way to the last row:

Rich (BB code):
=LINEST(INDEX($DU$3:$EN$83, 81, 0), $DU$2:$EN$2)

Does that help?
 
Last edited:
Upvote 0
iliace,

Interesting approach. However, in attempting to use CSE (Control, Shift, Enter) array formula in which I need the first two cells in row one of the results array, I haven't been able to find a VBA approach to copy the formula to additional rows. A single cell approach for entering the LINEST formula gives me just the slope. When I attempt to use your formula with the INDEX function I get a #Ref error. I need the slope and intercept for each row of the table for an additional computation. Not sure I understand how to incorporate the INDEX function into an array formula. When I use the formula "=LINEST(DU3:EN3:DU2:EN2,TRUE, FALSE)" in Cell 1, Row 1 of a previously selected 2x1 results matrix (and enter the formula using CSE) I get exactly the slope and intercept for that row. All the additional approaches I've tried to in order to repeat the 2x1 formula/results matrix in additional rows, results in displaying rest of the 2x5 array formula below the single row of results that I need.

Again, thanks for the approach (and I've thought of other ways I can use it my current project), but not sure I can apply it for my use of the LINEST function as an array formula.

jaustin
 
Upvote 0
If you simply copy down the 1x2 slope / intercept array, it works correctly. So where does the VBA come in? If you are trying to copy the formula, it will also work in VBA. If you are recreating the formula from scratch, use FormulaArray property of Range (instead of just Formula), to create a new formula.

Code:
Range("EO4:EP4").FormulaArray = "=LINEST(DU4:EN4, DU$2:EN$2)"
 
Upvote 0
If I highlight the 2x1 matrix that already has results in it and copy it to the next row below, I get the next two rows of the LINEST result (5x2 Matrix). What it shows I'm copying is the LINEST formula in braces. Haven't figured out how to copy. I keep getting "You can't change part of an array.

Thanks,

jaustin
 
Upvote 0
When you say 2x1, is the 2 number of rows or columns? Here is an example I used, and then LINEST in EO:EP. I typed the LINEST in EO3:EP3, Ctrl+Shift+Enter, and drag down. Each now has its individual LINEST result. I cannot replicate the behavior you're describing.

SlopeInter
123456789101112131415161718192010
4972402279281293646445306141872316446712-0.7355.10
3599484232361327729227265726669814294945-0.2148.82
8576778111429489684612251766439942416215-1.7773.16
5545649065943337281384975844954220504867-0.4761.38
80914164341451718732139366661245999594580.8151.77
24991463371757388513547684977979604993962.4834.68
75172299254310763224344184326991228452140.4242.91
3380847562857436487024718037953768259244-0.5767.03
41394019389364743676723452925790536870100.8547.03
8362294580286527756490266949533889991344-0.1658.07
7835901133674862682453132537371641711932-1.4357.97
36322726172034172168883444405829255262871.9220.71
48177468585593364250586538528493604266260.2154.06
5486351775543782474167165593551326786421-0.5456.50
9769247218121436307390784647683812445237-0.5653.72
65105922278399745932682446442593501951650.0550.19
9321992245463339881032118074358289543032-0.1652.48
71523814956523833148493022286286545356500.0150.37

<colgroup><col span="20"><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
iliace,

First, thanks for taking the time and effort to show that your solution works. Second, I've followed your steps and still get the remainder of the LINEST result array in the cells I've dragged over. BTW, when is said 2x1, I was meaning 2 columns wide by 1 row. Maybe I'm using a convention wrong. For the record, I'm using an Excel 365 Pro Plus subscription. I guess there may be a setting somewhere deep in Excel that is causing my behavior but I haven't really set anything different than the normal defaults. I'm going to test this file (pretty big and complex Workbook - about 150MB) on a second computer running the same version of Excel. I also have computers running standard Office 365 Home on which I will also try the same file. As a manual solution for my computing environment, I entered the LINEST array formula in the 2x1 array for all my rows. As the table of data will be updated with new data sets, hopefully, the formulas will auto-update. But, I will continue to pursue why my environment is giving different results. So far, I haven't seen any issues in Excel 2016 Pro Plus. In fact, I am completely blown away by its capabilities. Even though I'm running it on a 7th Gen i7 loaded HP, the speed at which it handles large 100,000 row Pivot Tables is astounding, at least for me. Even more surprising is that, just for fun, I loaded and executed this file on an early 4GB, i5 Surface Pro with about 108GB C:/ drive using Office 365 Home.

Again, thanks for taking the extra-ordinary efforts to pursue my issue. I've only signed up for MrExcel recently, but find it a very useful and helpful forum, particularly if you are an example of its users.

jaustin
 
Upvote 0
I've only signed up for MrExcel recently, but find it a very useful and helpful forum, particularly if you are an example of its users.
Thanks, that's nice of you to say! There are indeed many users here who will go to great lengths to help users, particularly with challenges that are well-explained, interesting in nature, or happen to be their subject matter expertise.


I've followed your steps and still get the remainder of the LINEST result array in the cells I've dragged over. BTW, when is said 2x1, I was meaning 2 columns wide by 1 row.

Can you try this? After you create the formula in the first row (e.g. EO3:EP3), then drag it, and you get the undesired result - go to one of the subsequent rows, select Go To (F5), select Special, and in the dialog box, select Current Array. Does it select only the current row, or the rows above it as well?

(shortcut key for Go To Current Array is Ctrl+/)
 
Last edited:
Upvote 0
iliace,

Tried to post but did something wrong. If I try and use the last row of the dragged 2x5 array, it unhighlights it and Current Array is just that part of the array (2 columns on row 5). If I leave the full 2x5 array highlighted after dragging down, and select Current Array it highlights the original row 1, two columns and unhiglights the remainder of the array. If I go to a different clear area and repeat with Stats = TRUE, I only get the original 2x1 results unless I drag down and then all the stats show. Originally, I thought I got everything when using Stats = True. Must be some operator error somewhere.

jaustin
 
Upvote 0
You should use Stats = False, or omit that argument altogether.
 
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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