Creating a VBA Macro to Individually Regress Columns B-J All Against Column O with DataAnalysis Tookpack

Rejexted

New Member
Joined
Oct 18, 2014
Messages
3
Hi guys and gals!

I've scoured the internet for a solution, but have yet to find a proper solution. What I want to do is quite simple: individually regress columns B-J (RAD to TPD) against Column O (Mkt-Rf) in my spreadsheet. I really want to use DataAnalysis, too, because it gives me everything I need for the CAPM finance model (Beta, Alpha, & T-Stat). My current code doesn't work, this is what it looks like:

Dim i As Integer
Dim stock_name As String
Dim y_Range As Range
Dim x_Range As Range

i = 1
Set y_Range = Range("B2:B123")
Set x_Range = Range("O2:O123")

For i = 1 To 9

stock_name = y_Range.Cells(1, 1)


Application.Run "ATPVBAEN.XLAM!Regress", ActiveSheet.Range(y_Range), _
ActiveSheet.Range(x_Range), False, True, , stock_name, False, False, False _
, False, , False

y_Range = y_Range.Offset(0, 1).Select

Next i

End Sub



This is what my data looks like (I'm doing a linear regression for CAPM, for all you finance heads out there)

DateRADWAGCVSARBRFAYMSIIECKBIGBTPDDateS&P 5003-Mo T-BillMkt-Rf
5/29/1987-4.53%1.03%3.05%-15.49%-1.45%4.76%5/29/19870.60%5.67%-5.07%
6/30/1987-4.06%12.24%8.50%0.00%2.94%6.82%6/30/19874.79%5.69%-0.90%
7/31/198710.81%2.12%8.52%6.67%-1.43%-3.19%7/31/19874.82%6.04%-1.22%
8/31/19871.49%-1.19%-1.57%-4.69%2.90%6.59%8/31/19873.50%6.40%-2.90%
9/30/1987-22.62%-4.21%-4.94%-16.39%-7.04%3.61%9/30/1987-2.42%6.13%-8.55%
10/30/1987-22.40%-23.51%-28.02%-29.41%-22.73%-29.35%10/30/1987-21.76%5.69%-27.45%
11/30/1987-0.16%-13.11%-12.35%-9.72%0.00%5.63%11/30/1987-8.53%5.77%-14.30%

<tbody>
</tbody>

I appreciate your help!

-Rejexted
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi and welcome to the MrExcel Message Board.

A couple of tweaks were required:
Code:
Sub Regression()

    Dim i As Integer
    Dim stock_name As String
    Dim y_Range As Range
    Dim x_Range As Range
    
    i = 1
    Set y_Range = Range("B2:B8")
    Set x_Range = Range("O2:O8")
    
    For i = 1 To 9
    
        stock_name = y_Range.Cells(1, 1).Offset(-1, 0)
        
        
        Application.Run "ATPVBAEN.XLAM!Regress", y_Range, _
        x_Range, False, True, , stock_name, False, False, False _
        , False, , False
        
        Set y_Range = y_Range.Offset(0, 1)

    Next i

End Sub

You tried to get the stock name from the first data row not the heading row mand you forgot the Set command when changing y_value to point to the next set of data.
 
Upvote 0
Thanks for the help, RickXL. I very much appreciate it.

I do have one more question for you, and it only just occurred to me after running this working code that you so kindly provided.

My returns data for MSII, ECK, BIGB, and TPD all start, end, or start and end at different dates than the full 5/30/1987 - 5/30/1997 time horizon. For example, ECK returns data was only available from 9/30/1993 to 2/28/1997.

How could I could tweak this code so that it will dynamically search for the first and last row in each column and only regress the data for that time horizon? For example, VBA would register that ECK starts late at 9/30/1993 and ends early on 2/28/1997, automatically changing the x_range & y_range to regress over the same shorter time horizon?

Again, thank you for your help. I really appreciate this community and will make sure to resolve this four year old thread which was my only previous resource beforehand.
 
Upvote 0
OK, try this:

Code:
Sub Regression()

    Dim i As Integer
    Dim stock_name As String
    Dim y_Range As Range
    Dim x_Range As Range
    Dim x_Temp As Range
    
    ' Set the name of the data sheet
    With Worksheets("Sheet1")
    
        ' Set the maximum X range
        Set x_Range = .Range("O2:O" & .Cells(.Rows.Count, "O").End(xlUp).Row)
        
        ' Step through columns
        For i = 1 To 9
        
            ' Set y range to first column, minus header
            Set y_Range = .Range("B2:B" & .Rows.Count).Offset(, i - 1)
            
            ' Get the stock name from the cell above the y range
            stock_name = y_Range.Cells(1).Offset(-1).Value
            
            ' Set the y range to only those cells that have data
            Set y_Range = y_Range.SpecialCells(xlCellTypeConstants)
            
            ' Make a temporary x range the same size as the y range
            Set x_Temp = Intersect(x_Range, y_Range.Offset(, x_Range.Column - y_Range.Column))

            ' Run the regression analysis
            Application.Run "ATPVBAEN.XLAM!Regress", y_Range, x_Temp, _
                False, True, , stock_name, False, False, False, False, , False
            
        Next i
        
    End With

End Sub

It assumes the x-range is in column O but works out the number of rows itself.
It needs to match the number of observations in the x and y ranges or else the regression analysis fails.
I have added some comments.
 
Upvote 0

Forum statistics

Threads
1,216,001
Messages
6,128,211
Members
449,435
Latest member
Jahmia0616

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