Results 1 to 3 of 3

Thread: [RESOLVED] [VBA] How to Determine the Last Item in a Collection Object

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2011
    Location
    Brisbane, Australia
    Posts
    43

    Resolved [RESOLVED] [VBA] How to Determine the Last Item in a Collection Object

    Basically, title says it all. I would have thought this to be a fairly easy thing to find, but I cannot find the answer to this in any documentation, or on any forums.

    This is my first time working with a collection object, but I understand it works similar to an array (although supposedly more optimized according to Chip Pearson who is somewhat of a VBA Guru).

    I tried using UBound() to get the last item, but apparently this only works for arrays. Is there a similar function to get the last item in a collection?

    To put it into context:

    Code:
    Sub Test()
    
        Dim Data As Collection
        Set Data = New Collection
        
        'Add items to collection
        Data.Add "A"
        Data.Add "B"
        Data.Add "C"
        
        'Remove last item from collection
        Data.Remove UBound(Data)     'This line does not work
    
    End Sub
    Any help is appreciated.

  2. #2
    Member
    Join Date
    Dec 2009
    Location
    Oregon
    Posts
    34

    Re: [VBA] How to Determine the Last Item in a Collection Object

    Code:
    Data.Remove Data.Count
    That should do it.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2011
    Location
    Brisbane, Australia
    Posts
    43

    Re: [VBA] How to Determine the Last Item in a Collection Object

    Yes, that works. I knew there had to be a simple solution.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width