You are on page 1of 3

Houston Seminar in Excel VBA Macros

Excel VBA Macro to calculate linear


correlation coefficients.

Sergio Perez Rodriguez


EG, PDG, MSc

July 2020
Sub Lin()

Dim varLinEst As Variant

Dim arg1, arg2 As Range

Set arg1 = Application.InputBox(Prompt:="Enter dependent variable data", Type:=8)

Set arg2 = Application.InputBox(Prompt:="Enter independent variable data", Type:=8)

varLinEst = WorksheetFunction.LinEst(arg1, arg2, True, True)

'This routine erases previous worksheets named LinEst

Dim ws As Worksheet

For Each ws In Worksheets

If ws.Name = "LinEst" Then

Application.DisplayAlerts = False

Sheets("LinEst").Delete

Application.DisplayAlerts = True

End

End If

Next

'Adding a worksheet named LinEst

Sheets.Add.Name = "LinEst"

Sheets("LinEst").Select

'Calculating and placing the linear coefficients in two cells of the new worksheet

Range("I3").Value = WorksheetFunction.Index(WorksheetFunction.LinEst(arg1, arg2), 1)

Range("J3").Value = WorksheetFunction.Index(WorksheetFunction.LinEst(arg1, arg2), 2)

End Sub
Bibliography

Jelen, B., Syrstad, T. 2016. Excel 2016 VBA and Macros. Pearson Education.

Roman, S. 2002. Writing Excel Macros with VBA. O’Reilly

Alexander, M. & Walkenbach, J. 2018. Excel VBA Programming For Dummies. Wiley

You might also like