Consulting

Results 1 to 5 of 5

Thread: VBA Excel Match Sets of items in a range

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    VBA Excel Match Sets of items in a range





    Hi folks,

    I am trying to match a set of items


    For each fruit in the array if the fruit has 2 ids its a matching set.


    Eg in this range > if Apple has both "SS" and "PP" then this is a set.



    I have tried a number of things and my logic is not working.



      
      
      
      
      Sub Set_Patterns()
      
      
      Dim i As Long
      Dim d As Range
      Dim Source As Worksheet
      
      Dim fruit As Variant
      
      
      Set Source = ActiveWorkbook.Worksheets("Test_Data")
      
      
      fruit = Array("Apple", "Banana", "Plum")
      
      
      For i = 0 To UBound(fruit)
      
            For Each d In Source.Range("A2:A10")
      
      
                  ' I did not know how to create another AND condition
    
                  If d = fruit(i) And d.Offset(0, 1) = "SS" And "PP" Then
                      d.Interior.ColorIndex = 3                           'red
                      d.Offset(0, 1).Interior.ColorIndex = 3              'red
                  End If
      
      
                ' Maybe an inner loop through the rows again? to identify the second ID
    
      
           Next d
      Next i




    fruit.png


    My goal is to match ONLY the sets.

    A fruit must match 2 conditions - SS & PP


    As you can see only Apple and Banana matched the 2 conditions - Apple has SS & PP, Banana has SS & PP.

    I would like to get the names of the fruits that have double conditions met.


    I will then put those into an array called matching_fruit_sets = array ("Apple", "Banana")


    Do i have to loop through the rows twice?


    please do take a look and thank you
    Last edited by dj44; 03-04-2023 at 09:33 AM. Reason: more clarification
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Maybe something like this

    Option Explicit
    
    
    Sub Set_Patterns()
    
    
        Dim i As Long
        Dim d As Range
        Dim Source As Worksheet
        Dim fruit As Variant
        
        Set Source = ActiveWorkbook.Worksheets("Test_Data")
        
        fruit = Array("Apple", "Banana", "Cherry")
        
        For i = LBound(fruit) To UBound(fruit)
            For Each d In Source.Range("A2:A10")
        
                ' maybe something like this <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                If d = fruit(i) Then
                    If d.Offset(0, 1) = "SS" Or d.Offset(0, 1) = "PP" Then
                        d.Interior.ColorIndex = 3                           'red
                        d.Offset(0, 1).Interior.ColorIndex = 3              'red
                    End If
                End If
            Next d
        Next i
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hi Paul,

    thank for your response.


    My goal is to identify the fruit names that ONLY have both coditions met.

    If a fruit has both IDS - SS & PP > thats my target set


    the highlighting is just for visual.

    How may I identify which fruits are a SET.

    I know its a bit tricky.

    I tried lots of ways.

    The issue is that the fruit repeats in multiple rows but the conditions are in 1 column.

    thats why its difficult.

    From the image the result is Apple and Banana as these have both conditions each.

    I hope I make some sense
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Sorry - I was only looking at your malformed AND

    This might be closer to what you were asking


    Option Explicit
    
    
    Sub Set_Patterns()
    
    
        Dim i As Long
        Dim d As Range
        Dim Source As Worksheet
        Dim fruit As Variant
        Dim rSS As Range, rPP As Range
        
        Set Source = ActiveWorkbook.Worksheets("Test_Data")
        
        fruit = Array("Apple", "Banana", "Cherry")
        
        For i = LBound(fruit) To UBound(fruit)
            Set rSS = Nothing
            Set rPP = Nothing
        
            For Each d In Source.Range("A2:A10").Cells
                If d = fruit(i) Then
                    Select Case d.Offset(0, 1)
                        Case "SS"
                            Set rSS = d
                        Case "PP"
                            Set rPP = d
                    End Select
                End If
            Next d
                
            If Not (rPP Is Nothing Or rSS Is Nothing) Then
                rPP.Resize(1, 2).Interior.Color = vbRed
                rSS.Resize(1, 2).Interior.Color = vbRed
            End If
        Next i
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 03-04-2023 at 12:13 PM. Reason: Couple of tweaks
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Paul,

    this is absolutely phenomenal!


    You are a star as always!

    I did not even think of a select case.

    I also did some other methods that were 3 times as long and verbose.


    Thank you for your generous time and expertise in solving this.


    I hope you have a fantastic weekend!



    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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