• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Multiple Regression

Yash0071

New Member
Hello,

This is my first thread. Im doing Multiple regression and need the following:

y= Cells B2:B15;
X1:X4= Cells C2:F15


1. I want a VBA code to do regression analysis and get an output exactly like Excel Data->Data analysis->Regression (Output Summary)

Thank you.
 

Attachments

  • 44.xlsm
    18.4 KB · Views: 5
Hello, thanks for trying to help me. I got my result to convert Linest() matrix into columns '=TOCOL(LINEST(A2:A15,B2:E15,TRUE,TRUE))' I write a VBA snippet for this. Now I want to know does anyone know the formula to find Significance in excel. Next is RMSE=R-Square? If not what is the excel formula.
 
Try This!

Code:
Sub MultipleRegressionAnalysis()
    Dim ws As Worksheet
    Dim rngY As Range, rngX As Range
    Dim lr As Long
    
    ' Set worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your worksheet name
    
    ' Find last row of data
    lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    ' Define ranges for Y and X variables
    Set rngY = ws.Range("B2:B" & lr)
    Set rngX = ws.Range("C2:F" & lr)
    
    ' Perform regression analysis
    Call RegressionAnalysis(rngY, rngX)
End Sub

Sub RegressionAnalysis(rngY As Range, rngX As Range)
    Dim RegOutput As String
    
    ' Perform regression analysis
    RegOutput = Application.WorksheetFunction.LinEst(rngY, rngX, True, True)
    
    ' Display regression output
    MsgBox RegOutput
End Sub
 
Thank you! I'm always executing my other code and I get a debug error highlighted in yellow and the line is this 'RegOutput = Application.WorksheetFunction.LinEst(rngY, rngX, True, True)'. But I'll try this and get back.
 
Debug Error: Type Mismatch. AI tools try to overcome it and then Error 1004 comes up or Error 13 again Etc. Screenshot (160).pngScreenshot (161).png
 
Modified as required

Code:
Sub MultipleRegressionAnalysis()
    Dim ws As Worksheet
    Dim rngY As Range, rngX As Range
    Dim lr As Long
    
    ' Set worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your worksheet name
    
    ' Find last row of data
    lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    ' Define ranges for Y and X variables
    Set rngY = ws.Range("B2:B" & lr)
    Set rngX = ws.Range("C2:F" & lr)
    
    ' Perform regression analysis
    Call RegressionAnalysis(rngY, rngX)
End Sub

Sub RegressionAnalysis(rngY As Range, rngX As Range)
    Dim RegOutput As Variant
    
    ' Perform regression analysis
    RegOutput = Application.WorksheetFunction.LinEst(rngY, Application.Transpose(rngX), True, True)
    
    ' Display regression output
    MsgBox RegOutput(1, 1) ' This assumes the first element of the output array is the slope coefficient
End Sub
 
Thank you! My Excel is giving 'Product deactivated' message. Does pasting this code in the second line sort this problem please 'Application.EnableCancelKey = xlDisabled'?
 
Thank you! My Excel is giving 'Product deactivated' message. Does pasting this code in the second line sort this problem please 'Application.EnableCancelKey = xlDisabled'?
No, the line 'Application.EnableCancelKey = xlDisabled' will not solve the 'Product deactivated' message issue in Excel.
This message typically indicates a problem with the activation status of your Microsoft Office product.
You should ensure that your Office subscription or license is active and that there are no issues with your Microsoft account or activation key.
 
Thank you! But I'm still getting the debug error after trying modified code. Anyways, I have finished off my model with a different means using codes to Auto populate and =TOCOL(LINEST()).
 

Attachments

  • 1.xlsm
    49.8 KB · Views: 0
Thank you! But I'm still getting the debug error after trying modified code. Anyways, I have finished off my model with a different means using codes to Auto populate and =TOCOL(LINEST()).
Okay... you're still encountering an error with your code.
Debugging can be a bit tricky, but let's try to identify the issue. Can you provide more details about the specific error message you're receiving?
 
Hello,
Sorry for the delay. 'RegOutput = Application.WorksheetFunction.LinEst(rngY, Application.Transpose(rngX), True, True)' the same thing is highlighted in Error and I've tried to paste it in Module & Sheet View code but none works.
Thank you!
 
Hmmmm...Strange..lets try with modified code with some additional error handling:

Code:
Sub MultipleRegressionAnalysis()
    Dim ws As Worksheet
    Dim rngY As Range, rngX As Range
    Dim lr As Long
    
    ' Set worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your worksheet name
    
    ' Find last row of data
    lr = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    ' Define ranges for Y and X variables
    Set rngY = ws.Range("B2:B" & lr)
    Set rngX = ws.Range("C2:F" & lr)
    
    ' Perform regression analysis
    Call RegressionAnalysis(rngY, rngX)
End Sub

Sub RegressionAnalysis(rngY As Range, rngX As Range)
    Dim RegOutput As Variant
    
    ' Perform regression analysis
    On Error Resume Next ' Resume execution if there's an error
    RegOutput = Application.WorksheetFunction.LinEst(rngY, Application.Transpose(rngX), True, True)
    On Error GoTo 0 ' Disable error handling
    
    If IsArray(RegOutput) Then
        ' Display regression output
        MsgBox "Slope coefficient: " & RegOutput(1, 1) ' This assumes the first element of the output array is the slope coefficient
    Else
        MsgBox "Error occurred during regression analysis."
    End If
End Sub
 
good Evening!

I tried that modified code and some progress is there as Debug error is not showing but a normal error is showing up.

Regards
 

Attachments

  • Screenshot (173).png
    Screenshot (173).png
    124.6 KB · Views: 2
  • 23.xlsm
    17 KB · Views: 4
Hi
Referring to Post #14 just above

These lines
Set rngY = ws.Range("B2:B15" & lr)
Set rngX = ws.Range("C2:G15" & lr)

Should be

Set rngY = ws.Range("B2:B" & lr)
Set rngX = ws.Range("C2:G" & lr)


and
RegOutput = Application.WorksheetFunction.LinEst(rngY, Application.Transpose(rngX), True, True)
should be

RegOutput = Application.WorksheetFunction.LinEst(rngY, rngX, True, True)
 
Back
Top