VBA-code with array-input and array output

heggalompen

New Member
Joined
Nov 29, 2017
Messages
1
Hi all,I am trying to program something in VBA. The inputs are matrices which varies on the amount of stocks, which is unfortunately not the same all the time.I already managed to get VBA to calculate 4 constants (A, B, C, D) with the matrices as input, there will always be only 4 constants, but they depend on the input of course. These 4 constants are calculated with the function letters (see attachment). The next thing is that I want to calculate minimum variance portfolio weights, which could be done with the following excel formula: MMULT(inverse covariance matrix, 1-vector) / C. I tried to program this into VBA with the help of google but it is not really working. I hope the problem is a bit clear by my text and the attachted excel file including the vba code letters and my attempts for the portfolio weights (minvar and minvar2).

Link to excel/vba-file:

https://files.fm/u/6djneetk#_All tips are more than welcome!Thanks in advance,
- Thomas
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the Forum!

To divide an array X by a constant C, you need to loop through each element, i.e, rather than saying: NewArray = X / C

So one way to do this:

L28:L33 = {=Minvar(C20:H25,I28:I33)}

Code:
Function MinVar(covar As Range, ones As Range) As Variant

    X = WorksheetFunction.MMult(covar, ones)
    C = WorksheetFunction.Sum(X)
    
    For i = 1 To UBound(X)
        X(i, 1) = X(i, 1) / C
    Next i
    
    MinVar = X

End Function

As an alternative to passing ones as an argument, you could also generate it in the function:

Code:
ones = Evaluate("Row(1:" & covar.Rows.Count & ")^0")


Excel 2010
CDEFGHIJKL
271-vectorResults
2839.2905713.465075.1004926.39995113.766654.6876121-0.02187
2913.46507118.7604-2.8944747.2442152.4666912.030871-0.19522
305.100492-2.8944717.10598-6.775-16.45195.27349710.21604
316.39995147.24421-6.77527.4167935.437863.43972910.36513
3213.7666552.46669-16.451935.4378696.51904-3.2794310.06807
334.68761212.030875.2734973.439729-3.279436.74309610.56784
341.00000
Sheet1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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