Creating a stack to push/pull ranges
Bob,
Nice! ... But..
A collection maintains an ordered list and the dictionary does not.
imo this makes a collection better suited for a stack then a dictionary.
so i knocked up following:
note: mIndex removed
note: Returns the actual range iso it's address
'CLASS MODULE CStack====================================
Option Explicit
Private mStack As Collection
Public Property Get Item(Optional ByVal idx As Long = -1)
With mStack
If Not IsEmpty Then
If idx = -1 Then
idx = .Count
End If
If IsObject(.Item(idx)) Then
Set Item = .Item(idx)
Else
Item = .Item(idx)
End If
End If
End With
End Property
Public Function Push(rng As Range)
mStack.Add rng
End Function
Public Function Pop()
mStack.Remove mStack.Count
End Function
Public Function Count() As Long
Count = mStack.Count
End Function
Public Function IsEmpty() As Boolean
IsEmpty = (Count = 0)
End Function
Private Sub Class_Initialize()
Set mStack = New Collection
End Sub
'NORMAL MODULE===========================================
Sub foo()
'Dim oStack as CStack
'Set oStck = New CStack
'With oStack
With New CStack 'cool syntax?!
.Push Range("A1:B5")
.Push Range("M1:P20")
MsgBox .Item.Address & vbLf & _
"Stack contains:" & vbLf & _
.Item(2).Address & vbTab & _
.Item(1).Address
.Pop
MsgBox .Item.Address
.Pop
MsgBox .IsEmpty
End With
End Sub
|