VBA data analysis loop problem

bobshah2010

New Member
Joined
Nov 29, 2016
Messages
39
Hi there,

I'm after a macro that can loop through a data table that i've prepared so that Excel can run multiple regressions at the same time using the "Data Analysis Tool pack"

So current my data table is set up so that every dependent variable is right before the subsequent independent variables im trying to measure against. Take for example the 3 types of dependent variables im trying to measure are: Sales, Revenue and Cost.
My dependent variables are A, B all through to Z.

My Table is set up as follows:
ABCDEFGHIJKLMN
SalesABRevenueABDCostHZSalesAHZ
1014200143503510135
207322573480332207332
30352503551003230332

<tbody>
</tbody>

I would like the macro to cycle through this table from Left to Right and generate a Regression Analysis output for every Dependent variable (Sales, Revenue, Cost) - i.e generate a result for Sales with A and B as independent variables, then perform same output below for Revenue with A, B and D, Then beneath that output do the same for Cost with H, Z and again for Sales with A, H, Z etc. etc.

Currently this is the code I have written out so far to generate the table. Note that the Dependent Variable will always be fixed and for this example assume the Dependent variable will always be Sales, Revenue and Cost.

As i'm sure you are aware - one of the limitation with Excel is that it can only perform a regression on independent variables that are directly beside the dependent variable - which is why I had to create a macro to generate a table to assist excel in looping through and generating Regression analyses.

Code:
Sub RunRegression()

Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, s1 As Worksheet, lr As Long, lc As Integer
Dim c As Integer, c1 As Integer, r As Long, lc2 As Integer, rng As Range, lr2 As Long, lc3 As Integer, i As Long


Set ws1 = Sheets("Data Table")
Set ws2 = Sheets("Regression name List")
Set ws3 = Sheets("Regression Table")
Set s1 = Sheets("Regression Test")


s1.Range("M30:M1000").Copy ws2.Range("A1")


'Remove all blank cells


ws2.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete


'clear regression table each run


Sheets("Regression Table").Cells.Clear


'create regression table from Data table to set up regression table to allow for regression to be peformed.


lr = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lc = ws1.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
lc2 = ws3.Cells(1, ws1.Columns.Count).End(xlToLeft).Column
For r = 1 To lr
    For c = 1 To lc
        If ws2.Cells(r, 1).Value = ws1.Cells(1, c).Value Then
            ws1.Columns(c).Copy
            ws3.Cells(1, lc2).PasteSpecial Paste:=xlPasteValues
           
        lc2 = lc2 + 1
        
        End If
        
    Next c
Next r


' Example of a for loop used to cycle through and perform regression


'    For i = 1 To 3
'
'        Application.Run "ATPVBAEN.XLAM!Regress", .Cells(1, .Column("C") + i).Resize(61), _
'        .Cells(1, .Column("AC") + i).Resize(61), False, True, , _
'        .Cells(109 + i * 20, .Column("C") + i), False, False, False, False, , False
'    Next i




End Sub


I've also included a snippet of something i found on the internet that shows someone using a for loop to cycle through a regression analysis. This may assist you in helping me find a solution for my problem.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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