Results 1 to 8 of 8
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Append one array to another

    Trying to make an array that has two merged (so appending one array to another)



    so far I have
    Code:
    'get one array and attach it to another array
    Function appendArray(ByRef ArrayGrowth As Variant, ByRef arrayToAppend As Variant) As Long()
    
    
    Dim arr() As Long
    Dim x As Long, g As Long, y As Long, z As Long
    
    
    
    
    g = UBound(ArrayGrowth) + UBound(arrayToAppend)
    z = UBound(ArrayGrowth)
    Debug.Print "Total: " & g
    
    
    For x = 0 To g
    
    
    ReDim Preserve arr(0 To x) As Long
    
    
    If x >= z Then
    arr(x) = arrayToAppend(y)
    y = y + 1
    Else
    arr(x) = ArrayGrowth(x)
    End If
    
    
    Next
    
    
    Debug.Print "count finished " & UBound(arr)
    For x = 0 To UBound(arr)
    Debug.Print arr(x)
    Next
    
    
    
    
    End Function
    I'd like a better way - the major problem I have is it isn't counting the 0 in the array (the first value).

  2. #2
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    eh nevermind I used this


    Code:
    'get one array and attach it to another array
    Function appendArray(ByRef ArrayGrowth As Variant, ByRef arrayToAppend As Variant) As Long()
    
    
    Dim arr() As Long
    Dim x As Long, i As Long
    
    
    For i = LBound(ArrayGrowth) To UBound(ArrayGrowth)
       ReDim Preserve arr(0 To i)
       arr(i) = ArrayGrowth(i)
       Debug.Print arr(i)
    Next
    
    
    For x = LBound(arrayToAppend) To UBound(arrayToAppend)
        i = i + 1
        ReDim Preserve arr(0 To i)
        arr(i) = arrayToAppend(x)
        Debug.Print arr(i)
    Next
    
    
    appendArray = arr()
    
    
    End Function
    however if you have a better way please post!

  3. #3
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    If both arrays are always one-dimensional, then this is how I'd go about it as well.

    CPearson has a bunch of UDF's that you could use for arrays if you're curious.
    http://www.cpearson.com/excel/vbaarrays.htm

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by thebigthing313 View Post
    If both arrays are always one-dimensional, then this is how I'd go about it as well.

    CPearson has a bunch of UDF's that you could use for arrays if you're curious.
    http://www.cpearson.com/excel/vbaarrays.htm

    *Gasp*

    *takes notes and runs off with them*

    great functions - hopefully they're all good

    THANKS!

  5. #5
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    I'd say CPearson has enough credibility to be reliable :P

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by thebigthing313 View Post
    I'd say CPearson has enough credibility to be reliable :P
    Yeah I'm going to test them out today anyway. My function failed when I passed an empty array - and anything I wrote to check if it was empty failed too.

  7. #7
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    One thing I would like to still be able to do is find a matching value in an array... so if there are two number 42 in an array I can remove or not add it to a new array

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Found some code
    Code:
    Function eliminateDuplicate(ByRef poArr() As Long) As Long()
    Dim poArrNoDup() As Long
    Dim dupBool As Boolean
    Dim dupArrIndex As Integer, i As Integer, j As Integer
    
    
    dupBool = False
    dupArrIndex = -1
    For i = LBound(poArr) To UBound(poArr)
            For j = LBound(poArr) To i
                If poArr(i) = poArr(j) And Not i = j Then
                    dupBool = True
                End If
            Next j
    
    
            If dupBool = False Then
                dupArrIndex = dupArrIndex + 1
                ReDim Preserve poArrNoDup(dupArrIndex)
                poArrNoDup(dupArrIndex) = poArr(i)
            End If
    Next i
    
    
    eliminateDuplicate = poArrNoDup
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Need help with array
    By programmingbeginner in forum Programming
    Replies: 14
    Last Post: 08-28-2014, 01:30 PM
  2. Replies: 6
    Last Post: 02-10-2014, 07:43 AM
  3. Replies: 1
    Last Post: 10-06-2011, 08:37 AM
  4. Replies: 7
    Last Post: 07-21-2011, 01:01 PM
  5. How to use array? [ solved] Thanks.
    By wasim_sono in forum Programming
    Replies: 0
    Last Post: 10-20-2006, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums