excel vba stack

'VBA does not include a native STACK (LIFO: LAST-In-FIRST-Out) memory 
'structure. It is possible to implement one using nothing but a native
'array or collection.

'However, the easiest solution is to have VBA create and use a .NET Stack:
Set stack = CreateObject("System.Collections.Stack")
Stack.Push "I"  
Stack.Push "was"  
Stack.Push "here."  
  
MsgBox Stack.Pop			'<--displays: here.
'Note:  Pop() removes the top (last-added) item from the Stack.
  
'  		Peek() will return the top value without removing it:
MsgBox Stack.Peek  			'<--displays: was

'Stack interrogation:  
MsgBox Stack.Count			'<--displays: 2.  Count() returns 0 if Stack empty.
MsgBox Stack.Contains("I")	'<--displays: True

'Stack to 1D VBA array (zero based):
arr = Stack.toArray
MsgBox arr(0)				'<--displays: I   Stack is unaltered.
  
'Clone the Stack:
Set Stack2  = Stack.Clone	'<--Stack2 is not affected by ops on Stack.
    
'Empty the Stack:
Stack.Clear  				'<--Stack now empty, but can take a new Push().
    
    
'NB:	Stacks can hold simple values and also complex objects.
    
'Reference: 
'    https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack
 
'FILO and LIFO are interchangeable and mean the same thing.

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source