Excel VBA Linear Regression for Every Four-Value Combination
Question details
The user needs to automate the calculation of a linear regression for every possible four-value combination from a dataset of 12 paired measurements, predicting a specific value for each combination.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Performing statistical data analysis where manual calculation of 495 subset combinations is too tedious, requiring a VBA macro to iterate through data points.
- Observed behavior
- Manual evaluation is inefficient, and previous VBA attempts failed with compilation errors due to incorrect loop limits, hard-coded subsets, and the use of the non-existent Math.Combinations function.
Ensure you have the Developer tab enabled in your spreadsheet program to access the VBA editor, and verify that your 12 data pairs (e.g., penetration and moisture) are arranged in two contiguous columns.
Use Nested VBA Loops and the LINEST Function
Implement four nested loops to dynamically generate every four-row combination, calculate the regression using LINEST, and output the predicted results.
To evaluate exactly four observations out of twelve without repetitions or missing sets, four nested For loops provide a reliable and logic-sound structure. By incrementally staggering the start value of each inner loop, you naturally generate all 495 combinations.
Avoid using hard-coded arrays or attempting to call undefined libraries like Math.Combinations, which lead to compilation errors.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. Go to Insert > Module to create a new blank script area.
Write four nested 'For' loops to iterate through the 12 rows. The first loop runs from 1 to 9, the second from the first loop's index + 1 to 10, the third from the second loop's index + 1 to 11, and the fourth from the third loop's index + 1 to 12.
Inside the innermost loop, declare and fill a 4-element X array and a 4-element Y array. Assign the worksheet values from the rows matching your four current loop variables to these arrays.
Use 'Application.WorksheetFunction.LinEst(Y_array, X_array)' to calculate the regression. Assign this to a Variant variable to extract the slope (index 1) and the intercept (index 2).
Calculate the predicted moisture at a penetration of 20 using the formula '(Slope * 20) + Intercept'. Use 'Debug.Print' to output the combination indices and the result to the Immediate Window, or write it directly to a new worksheet range.

Automate Complex Statistical Analysis with WPS Spreadsheet Macros
WPS Office Spreadsheet provides excellent support for VBA and macros, allowing you to run complex nested loops, arrays, and statistical functions like LINEST effortlessly.
- 1. Open Your Data: Launch WPS Spreadsheet and open the workbook containing your 12 paired measurements.
- 2. Access the Developer Tools: Click on the 'Developer' tab in the top ribbon and select 'VBA Editor'.
- 3. Paste and Run Your Macro: Insert a new module, paste your nested-loop LINEST code, and click 'Run' to instantly generate all 495 combination predictions.

Frequently Asked Questions
Why does Math.Combinations cause a compilation error in VBA?
VBA does not have a built-in Math.Combinations library or function. To generate mathematical combinations, you must either write a custom recursive function or use nested loops to iterate through the dataset manually.
How do I extract the slope and intercept from the LINEST function in VBA?
When you call WorksheetFunction.LinEst in VBA, it returns an array containing the regression statistics. By assigning the result to a Variant variable, you can retrieve the slope from the first index (e.g., Result(1)) and the y-intercept from the second index (e.g., Result(2)).
Is there a faster way to process 495 combinations without crashing Excel?
Processing 495 loops is extremely fast for VBA and should not crash the application. However, to optimize performance, write all output data to an array in memory first, and dump the entire array onto the worksheet at the end of the macro rather than writing to cells one by one inside the loop.




