[英]Add Object to beginning of collection VBA
This may have been asked before but I can't seem to find it... 这可能以前曾被问过,但我似乎找不到。
I have a class I created and a collection for each object of that class, when I added these objects to my collection I did not give each item a key and rather let VBA do that automatically. 我有一个我创建的类以及该类每个对象的集合,当我将这些对象添加到集合中时,我没有给每个项目一个键,而是让VBA自动执行。
Code below doesn't seem to work for me... 下面的代码似乎不适用于我...
ediData.StyleCollection.Add StoreStyleData(Range("A" & i & ":M" & i)), Before:=0
Tried this as well and not working either! 也尝试过这个,也不起作用!
ediData.StyleCollection.Add StoreStyleData(Range("A" & i & ":M" & i)), Before:=1
QUESTION: 题:
How can I add an item to the beginning of a collection rather than the end? 如何将项目添加到集合的开头而不是结尾? If I can't do that, how can I loop through a collection from the end instead of the start of it?
如果无法执行此操作,如何从末尾而不是从头开始遍历一个集合?
EDIT: 编辑:
Below I've included the Function for StoreStyleData
下面我包括了
StoreStyleData
的功能
Private Function StoreStyleData(rng As Range) As cPOStyle
Set StoreStyleData = New cPOStyle
With rng
StoreStyleData.XRef = .Cells(1).value
StoreStyleData.Season = .Cells(2).value
StoreStyleData.Style = .Cells(3).value
StoreStyleData.Color = .Cells(4).value
StoreStyleData.Size = .Cells(5).value
StoreStyleData.RetailPrice = .Cells(6).value
StoreStyleData.Category = .Cells(8).value
StoreStyleData.PO850Price = .Cells(9).value
StoreStyleData.XRefPrice = .Cells(10).value
StoreStyleData.Units = .Cells(11).value
StoreStyleData.SubTotal = .Cells(12).value
StoreStyleData.Description = .Cells(13).value
End With
End Function
COLLECTION WITHIN THE CLASS 在课堂上收集
''''''''''''''''''''''
' StyleCollection Property
''''''''''''''''''''''
Public Property Get StyleCollection() As Collection
If pStyleCollection Is Nothing Then Set pStyleCollection = New Collection
Set StyleCollection = pStyleCollection
End Property
Public Property Let StyleCollection(value As Collection)
Set pStyleCollection = value
End Property
Maybe try: 也许尝试:
StyleCollection.Add Item:=StoreStyleData(Range("A" & i & ":M" & i)), Before:=1
Does the below allow you to loop through collection backwards: 下面是否允许您向后循环浏览集合:
For i = StyleCollection.Count to 1 Step -1
'Debug.print StyleCollection.Items(i) or StyleCollection(i)
Next i
Untested, written on mobile. 未经测试,写在手机上。
Edit 1: 编辑1:
If StyleCollection.Count > 0 then
StyleCollection.Add Item:=StoreStyleData(Range("A" & i & ":M" & i)), Before:=1
Else
StyleCollection.Add StoreStyleData(Range("A" & i & ":M" & i))
End if
If you want to get last item first, use backward loop: 如果要首先获取最后一个项目,请使用向后循环:
Dim x As Integer
Dim cls As New MyClass
Dim itm As Item
For x = MyClass.Items.Count To 1 Step -1
Set itm = MyClass.Items(x)
Next
EXAMPLE 例
WorkersCollection
class: WorkersCollection
类:
Private col As New Collection
Sub AddWorker(w As Worker)
col.Add w
End Sub
Property Get Workers() As Collection
Set Workers = col
End Property
Worker
class: Worker
类:
Private m_Name As String
Property Get Name() As String
Name = m_Name
End Property
Property Let Name(v As String)
m_Name = v
End Property
Test method: 测试方法:
Sub Test()
Dim w As Worker
Dim x As Integer
Dim wcol As New WorkersCollection
For x = 1 To 5
Set w = New Worker
w.Name = "Name" & x
wcol.AddWorker w
Next
For x = wcol.Workers.Count To 1 Step -1
Debug.Print wcol.Workers(x).Name
Next
'Output:
'Name5
'Name4
'Name3
'Name2
'Name1
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.