ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Redim with string input (MATLAB Zeros function) (https://www.excelbanter.com/excel-programming/398100-redim-string-input-matlab-zeros-function.html)

Matthew Pfluger

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
http://www.mathworks.com/access/help...ref/zeros.html)

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?

Thanks,
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
Next

' 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

errorhandler:
Err.Raise 15

End Function
===============================================



All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com