Useful Function for Arrays

S

StFuji

I hate doing dim stuff for arrays:

Hence this function:

declare an array of what not:

dim myArray() as string
addtoarray myArray, "Some String"


* The isBlank function is far from perfect as is the addtoarray
function...I just find these useful
------------------------------------------------------------------------------------------------------------------------------

Public Function AddToArray(ByRef iArray As Variant, ByVal iVal As
Variant) As Variant

On Error GoTo ExitErr

BackIn:
ub = UBound(iArray)
If Not IsBlank(iArray(ub)) Then
ub = ub + 1
ReDim Preserve iArray(ub)
End If
iArray(ub) = iVal

ExitIt:
' Clean Up

Exit Function

ExitErr:
' Process Error
t = Err.Number
u = Err.Description

If Err.Number = 9 Then
ReDim iArray(0)
GoTo BackIn:
End If

GoTo ExitIt
End Function

Public Function IsBlank(ByVal expression As Variant) As Boolean
On Error GoTo ErrRt

IsBlank = False

If IsEmpty(expression) Then
IsBlank = True
ElseIf IsNull(expression) Then
IsBlank = True
ElseIf CStr(expression) = "" Then
IsBlank = True
End If

On Error GoTo ErrRt


ExitRt:
Exit Function

ErrRt:
MsgBox Err.Description
IsBlank = False
Resume ExitRt

End Function
 
R

RB Smissaert

If you have to do a lot of Redim Preserve then you might be better off using
a collection or a collection of arrays.

RBS
 

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

Top