Consulting

Results 1 to 6 of 6

Thread: Loop in selection

  1. #1
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    3
    Location

    Loop in selection

    Hi
    Could anyone please help me with this issue: I have sheet showing all of the treatments given to a group of animals.
    Some of the animals have received several treatments. I only want to keep treatments if the animal in question has been given treatment A.
    I would like to find out if there is a trend in the treatments eg. When an animal has been treated with A the animal has also been treated with D and treatment E.
    Please see example attached.

    How can I do this?



    Treatments.jpg
    Attached Images Attached Images

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    C2:=IF(COUNTIFS(A:A,A2,B:B,"A"),"Save","Delete")

  3. #3
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    3
    Location
    Hi mana

    Unfortunately this does not work, it comes with an error message invalid code.

  4. #4
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    thy this code,

    I have assumed the entries are in columns A and B and the results is pasted into columns F and G

    Sub movedata()
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    
    inarr = Range(Cells(2, 1), Cells(lastrow, 2))
    Range(Cells(2, 6), Cells(lastrow, 7)) = ""
    outarr = Range(Cells(2, 6), Cells(lastrow, 7))
    For i = 1 To lastrow - 1
       If inarr(i, 2) = "A" Then
         outarr(i, 1) = inarr(i, 1)
         outarr(i, 2) = inarr(i, 2)
        ' find all the other entries of this cow
         For j = 1 To lastrow - 1
           If i <> j Then
             If inarr(i, 1) = inarr(j, 1) Then
                outarr(j, 1) = inarr(j, 1)
                outarr(j, 2) = inarr(j, 2)
              End If
         End If
         Next j
       End If
     Next i
    Range(Cells(2, 6), Cells(lastrow, 7)) = outarr
     
       
       End Sub

  5. #5
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    3
    Location
    Thank you very much - this worked!

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Agnethe
    For the future, please post sample data in workbooks(Go Advanced/Manage Attachments), not as pictures
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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