VBA Append Function?

Discussion in 'AutoCAD' started by Dale Levesque, Jan 8, 2004.

  1. Is there a VBA function that performs the same task as the AutoLISP append
    function?

    Dale
     
    Dale Levesque, Jan 8, 2004
    #1
  2. Given that (append) works with LISTs, what
    counterpart are you using in VBA?

    If you need a list/array/vector type structure
    that must be dynamically appended to frequently,
    do not use arrays or REDIM PRESERVE, instead
    use a Collection object.
     
    Tony Tanzillo, Jan 8, 2004
    #2
  3. Thanks Tony. I've been using arrays and REDIM PRESERVE for ages. I was just
    wondering if there was a simple function to merge two arrays. I geuss I'll
    research the collection object.

    Best regards,

    Dale
     
    Dale Levesque, Jan 8, 2004
    #3
  4. Dale Levesque

    Mark Propst Guest

    Like Tony said, collections are WAY easier than arrays to mess with so I'm
    trying to get away from arrays all together.
    However a while back I was wondering the same thing as you so here's what I
    came up with.
    Pretty pathetic programming but you get the idea.
    only works with valid arrays at this point, haven't gotten the error
    trapping worked out
    and since collections are so much better, this is just a bit theoretical

    Sub AppendArray(ByRef vArray As Variant, vToAdd As Variant)
    Dim iStart As Integer
    Dim j As Integer
    If IsArray(vArray) And IsArray(vToAdd) Then
    If HasUBound(vArray) And HasUBound(vToAdd) Then
    iStart = UBound(vArray)

    ReDim Preserve vArray((UBound(vArray) - LBound(vArray)) +
    (UBound(vToAdd) - LBound(vToAdd)) + 1)
    For j = LBound(vToAdd) To UBound(vToAdd)
    vArray(iStart + j + 1) = vToAdd(j)
    Next j
    End If 'it has ubound (not an empty array)
    End If 'it is an array
    End Function

    Function HasUBound(varArray As Variant) As Boolean
    ' Determines whether an array contains a ubound
    'assume success
    HasUBound = True
    Dim lngUBound As Long
    On Error Resume Next
    lngUBound = UBound(varArray)
    If Err.Number <> 0 Then
    Err.Clear
    HasUBound = False
    End If
    End Function


    Sub test()
    Dim i As Integer
    Dim a1() As Integer
    ReDim a1(3)

    a1(0) = 1
    a1(1) = 2
    a1(2) = 3
    a1(3) = 4

    Debug.Print "A1 starts: "
    For i = 0 To UBound(a1)
    Debug.Print a1(i)
    Next

    Dim a2() As Integer
    ReDim a2(3)

    a2(0) = 5
    a2(1) = 6
    a2(2) = 7
    a2(3) = 8


    AppendArray a1, a2
    Debug.Print "After append, A1: "
    For i = 0 To UBound(a1)
    Debug.Print a1(i)
    Next


    End Sub
     
    Mark Propst, Jan 8, 2004
    #4
  5. Dale Levesque

    developer Guest

    Here is what I'm using:

    ' returns true if the argument array or list has been initialized
    Public Function IsBounded(ArgAry As Variant) As Boolean
    On Error Resume Next
    IsBounded = IsNumeric(UBound(ArgAry))
    End Function

    ' works like the AutoLISP (append expr ...) function
    ' appends arrays or lists together to form one array or list
    ' this might be changed to handle a variable number of arguments
    Public Function Append(Ary1 As Variant, Ary2 As Variant) As Variant
    Dim Element As Variant
    Dim Index As Integer
    Dim ReturnAry As Variant
    If IsBounded(Ary1) And IsBounded(Ary2) Then
    ReDim ReturnAry(0 To Length(Ary1) + Length(Ary2) - 1)
    For Each Element In Ary1
    ReturnAry(Index) = Element
    Index = Index + 1
    Next Element
    For Each Element In Ary2
    ReturnAry(Index) = Element
    Index = Index + 1
    Next Element
    Else
    MyError "Append: Arguments must both be arrays"
    End If
    Append = ReturnAry
    End Function
     
    developer, Jan 9, 2004
    #5
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 (here). After that, you can post your question and our members will help you out.