How to use Dynamic Ranges in Array Formulas using VBA

I am trying to write a macro that runs a regression analysis on a set of random variables and calculates the variance for a confidence interval. Specifically, I am trying to calculate [X'X]^-1. In this instance, X is a (# * 6) matrix and # varies depending on how much data the user has available. I have been successful using a static range, but am unable to figure out how to make it dynamic. The static code that works currently is:

Sheets("Forecast Calculations").Range("P2:U7").FormulaArray = _
     "=MINVERSE(MMULT(TRANSPOSE('Data for Regression'!R2C2:R172C7), 'Data for Regression'!R2C2:R172C7))"

My latest attempt at making it dynamic is as such:

Sheets("Data for Regression").Select
Dim RegDataEnd As Long
RegDataEnd = Columns("A").Find(what:="", after:=Cells(1, 1), LookIn:=xlValues).row - 1

Sheets("Forecast Calculations").Range("P2:U7").FormulaArray = _
    "=MINVERSE(MMULT(TRANSPOSE(Sheets("Data for Regression").Range(Cells(2, 2), Cells(RegDataEnd, 7))), Sheets("Data for Regression").Range(Cells(2, 2), Cells(RegDataEnd, 7)))"

But I receive the following error message:

Expected End of Statement:

And it highlights "Data in the Sheets() that follows transpose. Is there any way to do what I am trying to do?
Joe GantenbeinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Sheets("Forecast Calculations").Range("P2:U7").FormulaArray = _
     "=MINVERSE(MMULT(TRANSPOSE('" & Sheets("Data for Regression").Name & "'!" & _
     Sheets("Data for Regression").Range(Cells(2, 2), Cells(RegDataEnd, 7)).Address & _
     "),'" & Sheets("Data for Regression").Name & "'!" & _
     Sheets("Data for Regression").Range(Cells(2, 2), Cells(RegDataEnd, 7)).Address & "))"

Open in new window

Regards
Joe GantenbeinAuthor Commented:
Rgonzo1971,

That solved the "Expected End of Statement" error. However, I am now receiving "Run-time error '1004': Application-defined or object-defined error". You wouldn't happen to know what would be causing that, do you?

Thanks
Rgonzo1971Commented:
Hi,

pls try

   
    Sheets("Data for Regression").Select
    Dim RegDataEnd As Long
    RegDataEnd = Columns("A").Find(what:="", after:=Cells(1, 1), LookIn:=xlValues).row - 1

    Sheets("Forecast Calculations").Range("P2:U7").FormulaArray = _
        "=MINVERSE(MMULT(TRANSPOSE('" & Sheets("Data for Regression").Name & "'!" & _
        Sheets("Data for Regression").Range(Cells(2, 2), Cells(RegDataEnd, 7)).Address & _
        "),'" & Sheets("Data for Regression").Name & "'!" & _
        Sheets("Data for Regression").Range(Cells(2, 2), Cells(RegDataEnd, 7)).Address & "))"

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.