Serving the Quantitative Finance Community

 
User avatar
Beachcomber
Topic Author
Posts: 2
Joined: May 25th, 2004, 5:56 pm

vba question re: matrix formula

June 12th, 2013, 9:04 pm

I thought I was being clever and saving time by writing my code to utilize the solver optimization routine. However, I need to set up preliminary matrices and formulas. I am left trying to write a line like this in my code:Selection.FormulaArray = "=MINVERSE(R[-22]C:R[-3]C[19])"The problem is that I don't know the exact size of the matrix beforehand so I can't use this exact formulation. I've tried several things and everything has turned up to be lacking. Hopefully, I'm just forgetting something easy. Anybody know how to do this?
 
User avatar
Tad
Posts: 0
Joined: January 27th, 2010, 11:39 pm

vba question re: matrix formula

June 13th, 2013, 7:17 am

Have you tried using "Application.WorksheetFunction.MInverse(CustomRange)" (after setting the range CustomRange at runtime)?
 
User avatar
bluetrin
Posts: 2
Joined: September 9th, 2005, 6:41 am

vba question re: matrix formula

June 13th, 2013, 8:50 am

Dim rngInput As Range, rngStart As Range, rngoutput As Range Set rngStart = Range("B2") Set rngoutput = Range("S2") Set rngInput = Range(rngStart.Cells(1, 1), rngStart.Cells(3, 3)) Range(rngoutput.Cells(1, 1), rngoutput.Cells(rngInput.Rows.Count, rngInput.Columns.Count)) = Application.MInverse(rngInput) (if you use Application.WorksheetFunction.MInverse, you have to deal with the flimsy exception management of VBA)
Last edited by bluetrin on June 12th, 2013, 10:00 pm, edited 1 time in total.
 
User avatar
xpatagon
Posts: 0
Joined: June 1st, 2011, 1:31 pm

vba question re: matrix formula

June 13th, 2013, 2:55 pm

If your matrix is guaranteed to be surrounded by white space you could just use .currentrange on any cell you know to be inside it (eg the top left cell)
 
User avatar
Beachcomber
Topic Author
Posts: 2
Joined: May 25th, 2004, 5:56 pm

vba question re: matrix formula

June 13th, 2013, 5:48 pm

Thank you for the help. I fear there are still a couple of things that I don't understand.bluefin - I don't understand the following line... Set rngInput = Range(rngStart.Cells(1, 1), rngStart.Cells(3, 3))What is the (1,1) and (3,3) in reference to?xpatagon - my problem is not in finding the range of the target matrix. I need to write out the inverse matrix and preserve the formulas so that I can later use solver. This is what I am finding difficult.NOTE: In searching for solutions I have found allusions to being able to use solver with user defined variables, but I have found no sample code and have not been able to get it to work be messing around with the code.Thanks again for the help.
 
User avatar
Beachcomber
Topic Author
Posts: 2
Joined: May 25th, 2004, 5:56 pm

vba question re: matrix formula

June 13th, 2013, 7:13 pm

bluetrin - never mind, minor brain cramp. Though, this still doesn't preserve the formulas so I can use solver.Thank you.
 
User avatar
bluetrin
Posts: 2
Joined: September 9th, 2005, 6:41 am

vba question re: matrix formula

June 14th, 2013, 8:47 am

Sub ForSirBeachComber() Dim diagLen As Long diagLen = 3 Dim rngInput As Range, rngStart As Range, rngoutput As Range Set rngStart = Range("B2") Set rngoutput = Range("H2") Set rngInput = Range(rngStart.Cells(1, 1), rngStart.Cells(diagLen, diagLen)) If (rngoutput.HasArray) Then rngoutput.CurrentArray.ClearContents Range(rngoutput.Cells(1, 1), rngoutput.Cells(rngInput.Rows.Count, rngInput.Columns.Count)).FormulaArray _ = "=MInverse(" & rngInput.Address() & ")"End SubOne useful thing in VBA is that you can select keywords and methods by double clicking them and press F1 to see a fairly decent help.