+ Reply to Thread
Results 1 to 7 of 7

remove an object from a collection using the for each loop approach vs for i = loop

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    remove an object from a collection using the for each loop approach vs for i = loop

    Below are two methods of deleting an object from a collection.
    (1) uses a "for i = loop"
    (2) the other uses a "for each loop"

    I can remove the target object from the target collection using the for i = loop

    I do not understand what value should go into the Remove method
    for the "for each loop".

    The workbook ("Junk Test objects within a collection.xlsm") for the code is attached

    Below you will see both code approaches. They can be found in the module, Test_House_Class.

    The Test_House_Class module is dependent upon custom classes:
    aFloor is an instance of a Floor_Class
    FloorCollection is an instance of a Floor_CollectionClass

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: remove an object from a collection using the for each loop approach vs for i = loop

    Quote Originally Posted by whburling View Post
    Below you will see both code approaches. They can be found in the module, Test_House_Class.
    No, they aren't. Even adding in the code from your post above, the code in the module doesn't compile.

    Anyway, the Remove method of a Collection requires either an index number, or the key name (as it was added to the collection).
    Rory

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: remove an object from a collection using the for each loop approach vs for i = loop

    The only way to remove a member of a collection is using its index. You cannot remove members in a For Each loop. EDIT: As noted above you can also use the key, but you don't have the key when you do a For Each.

    I did not find any of this code in your attached file, BTW.
    Last edited by 6StringJazzer; 03-01-2023 at 07:00 PM.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: remove an object from a collection using the for each loop approach vs for i = loop

    Good Evening 6Stringer !!!!

    topic: "I did not find any of this code in your attached file, BTW
    * open the attached file
    * there appear to be 2 VBA Project (junk test collection within an object)
    would love to delete the first one but have no idea what i would be deleting; hence i have left it there
    * go to the second version
    * go to the modules folder
    * go to Test_House_Class
    dbl click
    go to line 86

    * go to class modules
    * find House_Class
    * find Floor_Class
    * Find Floor_CollectionClass

    Did that help? i am trying very hard to make your helping me easier. I appreciate all the help i get here in this forum. Thank you!!!!

  5. #5
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: remove an object from a collection using the for each loop approach vs for i = loop

    Thank you Rory !!!!

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: remove an object from a collection using the for each loop approach vs for i = loop

    Despite the valid comments of previous speakers, check the operation of this:
    Please Login or Register  to view this content.
    But this is the so-called "partisan".
    In subsequent tests, reduce the value of 2 in the red line to 1 and then to 0. Passing the code run in step mode (F8), observe in the Watches window the contents of the FloorCollection.

    Earlier I prepared another code for you to explain something. Remove my first suggestion and insert:
    Please Login or Register  to view this content.
    Learn to observe variables and objects in the Watches window and use the step-by-step mode of code execution. This makes it very easy to understand what is happening to variables or objects at any given time.

    Artik

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: remove an object from a collection using the for each loop approach vs for i = loop

    Quote Originally Posted by whburling View Post
    * there appear to be 2 VBA Project (junk test collection within an object)
    would love to delete the first one but have no idea what i would be deleting; hence i have left it there
    Seeing two VBA projects is an artifact of something going on in your specific environment. When I open your file, there is only one project, and it doesn't have this code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find and Replace Loop + a Loop to remove Duplicate values
    By lougs7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2018, 07:43 AM
  2. [SOLVED] Setting a collection of worksheets to feed to a For Each loop
    By JimDandy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2017, 04:31 PM
  3. [SOLVED] Look for a quicker code to replace this loop approach
    By billj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-29-2017, 08:03 AM
  4. loop through files, add to collcetion and process code from the collection
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-02-2017, 04:34 AM
  5. Using a For each loop with a column collection
    By DaProStudent in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-16-2017, 01:15 PM
  6. [SOLVED] For loop through Worksheets collection
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-31-2014, 08:23 AM
  7. Need Help on how to approach a possible if then loop
    By noggles301 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-19-2014, 08:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1