A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
A function can only return one value to the calling variable -MonthInHand in this case.
You also has to store CusStock in an array to manipulate the data. I have changed the function, so the last argument is the customer number for which to return the result.
If further testing is needed, it would be good with some data and expected results.
Function MonthsInHand(p_CusDemand As Range, p_CusStock As Range, p_PoolStock As Double, l_OutputIndex As Long)
Dim a_NumCusts As Integer 'Integer to hold number of customers being considered
Dim a_MIH As Variant 'declaring an array to recieve the result of a "months in hand" calculation (Stock divided by yearly demand)
Dim a_I As Integer
Dim a_X As Integer
Dim a_P As Integer 'Pool Stock counter
Dim a_MinIndex As Integer 'Minimum index counter
Dim a_CusStock As Variant 'Declare CusStock array
a_NumCusts = p_CusDemand.Cells.Count 'Storing the number of customers in the a_CusDemand range
ReDim a_MIH(a_NumCusts)
a_CusStock = p_CusStock.Value 'Store CusStock in the array
a_MinIndex = 1 'set minimum index counter to 1
a_P = p_PoolStock 'set the Pool stock counter to the initail value of Pool Stock
For a_X = 1 To p_PoolStock 'allocates the pool stock one item at a time to the customer stock with the lowest months in hand
For a_I = 1 To a_NumCusts 'calculats the current customer month in hand within the array a_MIH
If p_CusDemand(a_I) = 0 Then 'checks if the customer demand is 0, if so puts the 0 in the customer position in the array a_MIH
a_MIH(a_I) = 0
Else 'demand is greater than 0 it calculates the months in hand for that customer and puts in into the array a_MIH
a_MIH(a_I) = (a_CusStock(1, a_I) / p_CusDemand(a_I)) * 12
If a_MIH(a_I) < a_MIH(a_MinIndex) Then 'calculates which customer has the lowest months in hand and stores the array index value in a_MinIndex
a_MinIndex = a_I
End If
End If
Next
a_CusStock(1, a_MinIndex) = a_CusStock(1, a_MinIndex) + 1 'added 1 to the stock of the customer with the lowest months in hand
a_P = a_P - 1 'take 1 from the pool stock counter
Next
'p_Output = WorksheetFunction.Transpose(a_MIH) 'output array a_MIH in a range of cells passed through to the function
MonthsInHand = a_MIH(l_OutputIndex)
End Function