Consulting

Results 1 to 5 of 5

Thread: Iterating through an array, and omitting results

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location

    Iterating through an array, and omitting results

    If i am iterating through an array, and performing a test at each iteration for example:

    For Each customer in month1
       If customer = Gary & version = 1
       End if
    Next customer
    I want to do multiple iterations through the array, but omit the array elements that satisfied the first 'IF' statement. Can i do:

    For Each customer in month1
       If customer = Gary & version = 1
          customer = ""
       End if
    Next customer
    
    For Each customer in month1
       If customer = Gary & version = 2
       End if
    Next Customer
    Not the best example. Will the second 'FOR EACH' iterate through omitting the results that were picked up by 'customer = Gary' in the first loop. Hence not finding any results because Gary has already been counted in the first loop.

    Say the customers may have multiple versions, but i only want to count them in the first foreach.

    Im trying to get my head around it myself so if this makes no sense please let me know.

    Gary

  2. #2
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hi Gary

    I must admit it is not completely clear to me what you are doing, but I think just using a nested If could be what you want (by the way, i think you wanted And rather than the ampersand in your original):

    For Each customer in month1
       If customer = Gary Then
         If version = 1 Then      
           customer = ""
         Elseif version = 2 Then
           'do something else
          End If
       End if
    Next customer
    Richard

  3. #3
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location
    Quote Originally Posted by RichardSchollar
    Hi Gary

    I must admit it is not completely clear to me what you are doing, but I think just using a nested If could be what you want (by the way, i think you wanted And rather than the ampersand in your original):

    For Each customer in month1
       If customer = Gary Then
         If version = 1 Then      
           customer = ""
         Elseif version = 2 Then
           'do something else
          End If
       End if
    Next customer
    Richard
    Thanks, my point was can i use:

    customer = ""
    to clear an element in an 'customer' within my for each, for when i iterate through it again?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not combine them

    [vba]

    For Each customer in month1
    If customer = Gary And _
    (version = 1 OR version = 2) Then

    'do something
    End if
    Next Customer
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location
    Quote Originally Posted by xld
    Why not combine them

    [vba]

    For Each customer in month1
    If customer = Gary And _
    (version = 1 OR version = 2) Then

    'do something
    End if
    Next Customer
    [/vba]
    The idea behind what im doing is:

    I have an array of unique customers.
    I have a list in excel of customers and their versions.
    A customer may be running two versions, hence they may appear in the list more than once.
    I wish to iterate through the list using the array to find matches.

    Starting with version 3, i want to iterate through each customer in the list to find a match with the first customer in the array, if a matching customer is found, and they are running version 3 i wish to eliminate that customer from the array, so it doesnt get counted when i iterate through for version 2, as the first customer could be running both, and i only want to count version 3.

    Then iterate through the same list to find customers running version 2, but only if they havent already been picked up running version 3, if this makes sense.

    So i wondered if i could just wipe that customer from the array using
     customer = ""
    so when it iterated through the second time, even though that customer may well have been running version 2, it does not pick it up because it is erased from the array.

    I hope this makes more sense.

Posting Permissions

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