Table of Contents
The LinEst function in VBA (Visual Basic for Applications) is a powerful tool for calculating linear regression in Microsoft Excel. It allows users to easily determine the slope and intercept of a linear trendline, as well as other important statistics such as the coefficient of determination and standard error. To use the LinEst function, the user must first enable the “Analysis ToolPak” add-in. Then, the function can be called in the VBA code using the syntax “Application.WorksheetFunction.LinEst (known_y’s,known_x’s,[const],[stats])”. An example of using the LinEst function would be to analyze a set of sales data to determine the expected growth rate and estimated sales for the next quarter. By using the LinEst function, this process can be automated and the results can be easily obtained and utilized for decision making.
Use the LinEst Function in VBA (With Example)
You can use the LinEst method in VBA to fit a linear regression model.
This function uses the following basic syntax:
LinEst(Arg1, Arg2, Arg3, Arg4)
where:
- Arg1: Set of y-values
- Arg2: Set of x-values
- Arg3 (optional): TRUE = calculate intercept normally, FALSE = force intercept to be zero
- Arg4 (optional): TRUE = calculate additional regression statistics, FALSE = only calculate coefficients
The following example shows how to use the LinEst method to fit a regression model in VBA in practice.
Example: How to Use LinEst Function in VBA
Suppose we have the following dataset that contains one predictor variable (x) and one response variable (y):

We can create the following macro that uses the LinEst method to fit a simple linear regression model to this dataset:
Sub UseLinEst() Range("D1:E1") = WorksheetFunction.LinEst(Range("B2:B15"), Range("A2:A15")) End Sub
When we run this macro, we receive the following output:

The values in the output represent the coefficients for the regression model:
- The coefficient for β0 is 3.52169.
- The coefficient for β1 is 0.693717.
Using these values, we can write the equation for this simple regression model:
y = 3.52169 + 0.693717(x)
We interpret the intercept term to mean that when x is equal to zero, the average value of y is 3.52169.
If we would like to use the LinEst method to produce additional regression statistics, we can use the following syntax:
Sub UseLinEst() Range("D1:E5") = WorksheetFunction.LinEst(Range("B2:B15"), Range("A2:A15"),True, True) End Sub
When we run this macro, we receive the following output:

The following screenshot provides an explanation of each value in the output:

By supplying a value of True to the last argument of the LinEst method, we’re able to calculate several additional regression statistics.
Cite this article
stats writer (2024). How can I use the LinEst function in VBA with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-linest-function-in-vba-with-an-example/
stats writer. "How can I use the LinEst function in VBA with an example?." PSYCHOLOGICAL SCALES, 22 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-linest-function-in-vba-with-an-example/.
stats writer. "How can I use the LinEst function in VBA with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-linest-function-in-vba-with-an-example/.
stats writer (2024) 'How can I use the LinEst function in VBA with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-linest-function-in-vba-with-an-example/.
[1] stats writer, "How can I use the LinEst function in VBA with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the LinEst function in VBA with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
