The VBA Queue represents a first-in, first-out collection of objects (FIFO). You push items to the Queue, and then read them out at a later time in the same order. Queues are useful if you want to store items in the exact order you should process them, especially if you constantly are adding new items to the Queue.
VBA Queue example
Below is an example of a FIFO queue in VBA. Four strings are added and then the first one is removed.
Dim queue as Object Set queue = CreateObject("System.Collections.Queue") 'Create the Queue queue.Enqueue "Hello" queue.Enqueue "There" queue.Enqueue "Mr" queue.Enqueue "Smith" peekAtFirst = queue.Peek() 'Result" "Hello" doesContain = queue.Contains("htrh") 'Result: False doesContain = queue.Contains("Hello") 'Result: True 'Get first item in Queue and remove it from the Queue firstInQueue = queue.Dequeue() '"Hello" 'Count items Debug.Print queue.Count 'Result: 3 'Clear the Queue queue.Clear Set queue = Nothing
Queues are useful when in need of dynamic data structure that changes size, while adding and removing elements from it in the same sequence. If you need the opposite i.e. a LIFO (last in first out), data structure check out the VBA Stack.