Sub PrependToArray( vArray As Variant, vNewValue As Variant )
' eg ... given vArray = 1,2,3,4
' vNewValue = 99
' returns 99,1,2,3,4
' The is returned in the vArray itself
%REM
' Sample call - Copy this bit to the initialize sub ========================
Dim strPreopleNamesArr(0 To 1) As String ' NOTE THAT Dim will not work, you will get an error Illegal REDIM of fixed array
strPreopleNamesArr(0) = "Mary" ' use REDIM and it works fine
strPreopleNamesArr(1) = "Jane"
Call PrependToArray( strPreopleNamesArr, "John" ) ' put the new value at the start
Print Join(strPreopleNamesArr, ", ")
' End Sample call - End of copy to initialize ===== =========================
%ENDREM
' Move all the values by 1 then add the new value at the front
Dim intIndex As Integer
Redim Preserve vArray(Ubound(vArray)+1)
For intIndex = Ubound(vArray) To 1 Step -1
vArray(intIndex)=vArray(intIndex-1)
Next
vArray(0) = vNewValue
End Sub
No comments:
Post a Comment