Redim with string input (MATLAB Zeros function)



I am trying to create a function that mimics MATLAB's zeros function where it
creates a variable of zeros (see

To do this, I use a Parameter Array to obtain the dimensions and sizes. For
example, to get a 2 X 2 array of zeros, you'd use the syntax "=matZeros(2,2)".

I'm trying to build a string based on the ParamArray, but that doesn't seem
to work. The code is below. Can anyone suggest a workaround?

Matthew Pfluger

Private Function matZeros(ParamArray vArr() As Variant) As Variant

Dim tempVar As Variant

If UBound(vArr) - LBound(vArr) = 0 Then GoTo errorhandler

Dim sDimensions As String
Dim iInc As Integer
sDimensions = ""
For iInc = LBound(vArr) To UBound(vArr)
If IsNumeric(vArr(iInc)) Then
sDimensions = sDimensions + ", 1 to " & vArr(iInc)
End If

' Remove leading comma
If Left(sDimensions, 1) = "," Then sDimensions = Right(sDimensions,
Len(sDimensions) - 2)

' Create array based on data type (if applicable)
If Not (IsNumeric(UBound(vArr))) Then
Select Case UBound(vArr())
Case "Single"
ReDim tempVar(sDimensions) As Single
Case "Double"
ReDim tempVar(sDimensions) As Double
Case "Integer"
ReDim tempVar(sDimensions) As Integer
Case "Long"
ReDim tempVar(sDimensions) As Long
Case Else
GoTo errorhandler
End Select
Else ' default to double if data type omitted
ReDim tempVar(sDimensions) As Double
End If

matZeros = tempVar

Exit Function

Err.Raise 15

End Function


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question