除非您可以使用Collection
對象,否則沒有內置或直接的方法來對數組執行此操作。
Public Sub TestArr()
Dim list As New Collection
list.Add "John"
list.Add "Mary"
list.Add "Bob"
list.Remove 1
End Sub
隨着陣列可以使用Preserve
關鍵字,以保持現有的數據ReDim
他們。
Public Sub TestArr()
Dim list() As Variant
'Creates a dynamic list of 10 items
ReDim list(1 To 10)
Dim i As Long
For i = 1 To 10
list(i) = "Item" & CStr(i)
Next i
'Append to the end of an array
AppendToArray list, "NotAnItem"
'Remove the 5th element
RemoveFromArray list, 5
End Sub
Public Sub AppendToArray(ByRef list() As Variant, ByVal item As Variant)
Dim n As Long
n = UBound(list)
' Make Room
ReDim Preserve list(1 To n + 1)
' Assign new value
list(n + 1) = item
End Sub
Public Sub RemoveFromArray(ByRef list() As Variant, ByVal index As Long)
Dim n As Long, i As Long
n = UBound(list)
' Move last items down by one
For i = index To n - 1
list(i) = list(i + 1)
Next i
ReDim Preserve list(1 To n - 1)
End Sub
如果我明白正確的話,您應該使用堆棧,這對於Java也是如此。在VBA中,您可以使用此創建一個 - > Set stack = CreateObject(「System.Collections.Stack」),其中stack是類型對象的變量。 –
https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx#Anchor_10 –