Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi,
two questions on Collections: 1. in VBA, the only way to copy the items (not just the references) of a collection c1 to another collection c2 is to go through each element of c1 with a For Each Next and Add each item to c2, right? 2. I have a collection c1 containing some objects to be deleted (the ones for which the Defective property is True). I want to delete them from c1 and at the same time to delete the objects in the same position from another collection c2. See code below: Sub RemoveDefectivesfromColns(c1 As Collection, c2 As Collection) Dim obj as Object, I As Long For I=1 to c1.Count Set obj =c1(I) If obj.Defective Then c1.Remove I c2.Remove I End If Next I End Sub Problems: number of elements in c1 and c2 is changing as the cycle goes on, so sooner or later the Remove method will fail (because I exceeds the number of remaining elements in c1 and c2). How can I solve this? Also, is there a way to do this with a For Each In Next? Would it be faster? Thanks, Best Regards deltaquattro c1.Add Item:="woof" Set c2=c1 makes |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Change this line:
For I=1 to c1.Count To this: For I = c1.Count To 1 Step -1 "deltaquattro" wrote in message ... Hi, two questions on Collections: 1. in VBA, the only way to copy the items (not just the references) of a collection c1 to another collection c2 is to go through each element of c1 with a For Each Next and Add each item to c2, right? 2. I have a collection c1 containing some objects to be deleted (the ones for which the Defective property is True). I want to delete them from c1 and at the same time to delete the objects in the same position from another collection c2. See code below: Sub RemoveDefectivesfromColns(c1 As Collection, c2 As Collection) Dim obj as Object, I As Long For I=1 to c1.Count Set obj =c1(I) If obj.Defective Then c1.Remove I c2.Remove I End If Next I End Sub Problems: number of elements in c1 and c2 is changing as the cycle goes on, so sooner or later the Remove method will fail (because I exceeds the number of remaining elements in c1 and c2). How can I solve this? Also, is there a way to do this with a For Each In Next? Would it be faster? Thanks, Best Regards deltaquattro c1.Add Item:="woof" Set c2=c1 makes |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi JLGWhiz,
nice, I didn't think of it! thanks, Best Regards deltaquattro On 11 Mar, 17:25, "JLGWhiz" wrote: Change this line: For I=1 to c1.Count To this: For I = c1.Count To 1 Step -1 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Re 1.
Yes, that is assuming your collection refers to objects (well, there are other ways but no need to go there). Re 2. Loop backwards For I= c1.Count to 1 step -1 Regards, Peter T "deltaquattro" wrote in message ... Hi, two questions on Collections: 1. in VBA, the only way to copy the items (not just the references) of a collection c1 to another collection c2 is to go through each element of c1 with a For Each Next and Add each item to c2, right? 2. I have a collection c1 containing some objects to be deleted (the ones for which the Defective property is True). I want to delete them from c1 and at the same time to delete the objects in the same position from another collection c2. See code below: Sub RemoveDefectivesfromColns(c1 As Collection, c2 As Collection) Dim obj as Object, I As Long For I=1 to c1.Count Set obj =c1(I) If obj.Defective Then c1.Remove I c2.Remove I End If Next I End Sub Problems: number of elements in c1 and c2 is changing as the cycle goes on, so sooner or later the Remove method will fail (because I exceeds the number of remaining elements in c1 and c2). How can I solve this? Also, is there a way to do this with a For Each In Next? Would it be faster? Thanks, Best Regards deltaquattro c1.Add Item:="woof" Set c2=c1 makes |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi, Peter,
thanks for the answers: 1. yes I forgot to specify that I was referring to a collection of objects. Which are the other way to do a "deep" copy which you are referring to? Could you give me some details? 2. Oh, right, didn't think of that :) thanks! Best Regards Sergio On 11 Mar, 17:32, "Peter T" <peter_t@discussions wrote: Re 1. Yes, that is assuming your collection refers to objects (well, there are other ways but no need to go there). Re 2. Loop backwards For I= c1.Count to 1 step -1 Regards, Peter T "deltaquattro" wrote in message ... Hi, two questions on Collections: 1. in VBA, the only way to copy the items (not just the references) of a collection c1 to another collection c2 is to go through each element of c1 with a For Each Next and Add each item to c2, right? 2. I have a collection c1 containing some objects to be deleted (the ones for which the Defective property is True). I want to delete them from c1 and at the same time to delete the objects in the same position from another collection c2. *See code below: Sub RemoveDefectivesfromColns(c1 As Collection, c2 As Collection) Dim obj as Object, I As Long For I=1 to c1.Count * * *Set obj =c1(I) * * *If obj.Defective Then * * * * * *c1.Remove I * * * * * *c2.Remove I * * *End If Next I End Sub Problems: number of elements in c1 and c2 is changing as the cycle goes on, so sooner or later the Remove method will fail (because I exceeds the number of remaining elements in c1 and c2). How can I solve this? Also, is there a way to do this with a For Each In Next? Would it be faster? Thanks, Best Regards deltaquattro c1.Add Item:="woof" Set c2=c1 makes |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sorry I meant assuming *not* objects, VB/A does not provide a 'clone' method
of copying objects. It is though possible to assign an object to an object variable knowing only the object-pointer to the object, IOW could store merely the objPtr (a long) to each object then reassign to variables later but I doubt useful for your purposes (involves memory copy and risky if not done correctly). You can easily assign an existing collection to a new collection variable, eg Set col1 = col2 however it remains the same collection object, add/remove to col1 and col2 reflects same. If you want to store references to all your objects in the collection while manipulating it in some other way, do exactly as you indicated in your OP. Or if very large and you don't need the 'keys' probably faster to populate an appropriately dimensioned and declared object array. Regards, Peter T "deltaquattro" wrote in message ... Hi, Peter, thanks for the answers: 1. yes I forgot to specify that I was referring to a collection of objects. Which are the other way to do a "deep" copy which you are referring to? Could you give me some details? 2. Oh, right, didn't think of that :) thanks! Best Regards Sergio On 11 Mar, 17:32, "Peter T" <peter_t@discussions wrote: Re 1. Yes, that is assuming your collection refers to objects (well, there are other ways but no need to go there). Re 2. Loop backwards For I= c1.Count to 1 step -1 Regards, Peter T "deltaquattro" wrote in message ... Hi, two questions on Collections: 1. in VBA, the only way to copy the items (not just the references) of a collection c1 to another collection c2 is to go through each element of c1 with a For Each Next and Add each item to c2, right? 2. I have a collection c1 containing some objects to be deleted (the ones for which the Defective property is True). I want to delete them from c1 and at the same time to delete the objects in the same position from another collection c2. See code below: Sub RemoveDefectivesfromColns(c1 As Collection, c2 As Collection) Dim obj as Object, I As Long For I=1 to c1.Count Set obj =c1(I) If obj.Defective Then c1.Remove I c2.Remove I End If Next I End Sub Problems: number of elements in c1 and c2 is changing as the cycle goes on, so sooner or later the Remove method will fail (because I exceeds the number of remaining elements in c1 and c2). How can I solve this? Also, is there a way to do this with a For Each In Next? Would it be faster? Thanks, Best Regards deltaquattro c1.Add Item:="woof" Set c2=c1 makes |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sorry I meant assuming *not* objects, VB/A does not provide a 'clone' method
of copying objects. It is though possible to assign an object to an object variable knowing only the object-pointer to the object, IOW could store merely the objPtr (a long) to each object then reassign to variables later but I doubt useful for your purposes (involves memory copy and risky if not done correctly). You can easily assign an existing collection to a new collection variable, eg Set col1 = col2 however it remains the same collection object, add/remove to col1 and col2 reflects same. If you want to store references to all your objects in the collection while manipulating it in some other way, do exactly as you indicated in your OP. Or if very large and you don't need the 'keys' probably faster to populate an appropriately dimensioned and declared object array. Regards, Peter T "deltaquattro" wrote in message ... Hi, Peter, thanks for the answers: 1. yes I forgot to specify that I was referring to a collection of objects. Which are the other way to do a "deep" copy which you are referring to? Could you give me some details? 2. Oh, right, didn't think of that :) thanks! Best Regards Sergio On 11 Mar, 17:32, "Peter T" <peter_t@discussions wrote: Re 1. Yes, that is assuming your collection refers to objects (well, there are other ways but no need to go there). Re 2. Loop backwards For I= c1.Count to 1 step -1 Regards, Peter T "deltaquattro" wrote in message ... Hi, two questions on Collections: 1. in VBA, the only way to copy the items (not just the references) of a collection c1 to another collection c2 is to go through each element of c1 with a For Each Next and Add each item to c2, right? 2. I have a collection c1 containing some objects to be deleted (the ones for which the Defective property is True). I want to delete them from c1 and at the same time to delete the objects in the same position from another collection c2. See code below: Sub RemoveDefectivesfromColns(c1 As Collection, c2 As Collection) Dim obj as Object, I As Long For I=1 to c1.Count Set obj =c1(I) If obj.Defective Then c1.Remove I c2.Remove I End If Next I End Sub Problems: number of elements in c1 and c2 is changing as the cycle goes on, so sooner or later the Remove method will fail (because I exceeds the number of remaining elements in c1 and c2). How can I solve this? Also, is there a way to do this with a For Each In Next? Would it be faster? Thanks, Best Regards deltaquattro c1.Add Item:="woof" Set c2=c1 makes |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
A List of Collections and Custom Collections | Excel Programming | |||
Removing a collection item via key name check | Excel Programming | |||
Collection Problems | Excel Programming | |||
Collection Class problems | Excel Programming | |||
Removing an Item From a Collection | Excel Programming |