# VBA array input array output problem

1. ## VBA array input array output problem

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).

All tips are more than welcome!
Thanks in advance! Job  Register To Reply

2. ## Re: VBA array input array output problem

As I explain here (https://www.excelforum.com/tips-and-...uild-udfs.html ), one of the first things I do when writing a UDF is to add a stop statement or a breakpoint so that I can enter debug mode. So the first thing I did with your file was to add a stop statement to each of the minvar() UDFs, then enter each into a cell on the spreadsheet.

Stepping through each function, it looks like the same error in both. In each function, you are attempting to divide an array (X or the result of the matrix multiplication) by a scalar (C) using the division operator. Unfortunately, it does not appear that the VBA division operator knows how to divide an array/matrix by a scalar.

The first solution that I see is to loop through each element of the array X (so the minvar approach will be better than minvar2) and divide by C one element at a time. Are you familiar with VBA loops (For..Next in particular? https://msdn.microsoft.com/en-us/VBA...next-statement )?  Register To Reply

#### Thread Information

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Tags for this Thread #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1