+ Reply to Thread
Results 1 to 9 of 9

Creating list based on 3 criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Creating list based on 3 criteria

    Hi,

    Please see attached.

    I need to create (via a macro) a seperate list on Sheet2 based on the values on Sheet1. If the entries in column A have a "A" "B" & "C" (in any order) in column B, then all 3 rows should be copied to Sheet2, as per the example.

    I'd be glad of any help. If a solution can be found, please post the code rather than an attachment. Thanks in advance.
    Attached Files Attached Files
    Last edited by bd528; 09-10-2010 at 01:46 PM.

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Creating list based on 3 criteria

    You could use this macro:

    Sub Macro1()
       Dim sh1 As Worksheet
       Dim sh2 As Worksheet
       Dim lastRow As Long, r As Long, myTest As String
       Dim myNum As String, sh2Row As Long
       
       Set sh1 = ThisWorkbook.Sheets("sheet1")
       Set sh2 = ThisWorkbook.Sheets("sheet2")
       
       sh2.Cells.ClearContents
       
       lastRow = sh1.Cells(Rows.Count, "a").End(xlUp).Row
     
       sh2Row = 1
       For r = 1 To lastRow
          If sh1.Cells(r, 1) <> myNum Then
             myTest = "..."
             myNum = sh1.Cells(r, 1)
          End If
          Mid(myTest, Asc(sh1.Cells(r, 2)) - 64, 1) = " "
    
          If InStr(myTest, ".") = 0 Then
             sh1.Range(r - 2 & ":" & r).Copy sh2.Cells(sh2Row, 1)
             sh2Row = sh2Row + 3
          End If
       Next r
    End Sub
    Regards,
    Antonio

  3. #3
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Creating list based on 3 criteria

    Hi,

    Thats works perfectly - thanks.

    How can I get this to work if I change the values in row B? For example all the A's are "Mouse", all the B's are "Cat" and all the C's are "Dog".

    Thanks again.

  4. #4
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Creating list based on 3 criteria

    You could try with this macro (you can put B values in any order and the match is not case sensitive):

    Sub Macro1()
       Dim sh1 As Worksheet
       Dim sh2 As Worksheet
       Dim lastRow As Long, r As Long, myTest As String
       Dim myNum As String, sh2Row As Long
       
       Set sh1 = ThisWorkbook.Sheets("sheet1")
       Set sh2 = ThisWorkbook.Sheets("sheet2")
       
       sh2.Cells.ClearContents
       
       lastRow = sh1.Cells(Rows.Count, "a").End(xlUp).Row
     
       sh2Row = 1
       For r = 1 To lastRow
          If sh1.Cells(r, 1) <> myNum Then
             myTest = "Mouse,Cat,Dog"
             myNum = sh1.Cells(r, 1)
          End If
          myTest = Replace(LCase(myTest), LCase(sh1.Cells(r, 2)), "")
    
          If myTest = ",," Then
             sh1.Range(r - 2 & ":" & r).Copy sh2.Cells(sh2Row, 1)
             sh2Row = sh2Row + 3
          End If
       Next r
    End Sub
    Regards,
    Antonio

  5. #5
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Creating list based on 3 criteria

    Antonio,

    Again, that works perfectly. I have thought of one final change, if you have any suggesions...?

    Currently, the value in column A must have mouse, cat, and dog in column B before it is copied to Sheet2. It would be good if the column A figure figure went to Sheet2 if it only has mouse as well - as per the example attached.

    This is the last change - honestly

    Thanks so much.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Creating list based on 3 criteria

    You could use this code:
    Sub Macro1()
       Dim sh1 As Worksheet
       Dim sh2 As Worksheet
       Dim lastRow As Long, r As Long, myTest As String
       Dim myNum As String, sh2Row As Long, startRow As Long
       
       Set sh1 = ThisWorkbook.Sheets("sheet1")
       Set sh2 = ThisWorkbook.Sheets("sheet2")
       
       sh2.Cells.ClearContents
       
       lastRow = sh1.Cells(Rows.Count, "a").End(xlUp).Row
     
       sh2Row = 1
       For r = 1 To lastRow + 1
          If sh1.Cells(r, 1) <> myNum Then
             If myNum <> "" And myTest = "" Then
                sh1.Range(startRow & ":" & r - 1).Copy sh2.Cells(sh2Row, 1)
                sh2Row = sh2Row + r - startRow
             End If
             myTest = "Mouse"
             myNum = sh1.Cells(r, 1)
             startRow = r
          End If
          myTest = Replace(LCase(myTest), LCase(sh1.Cells(r, 2)), "")
     
       Next r
    End Sub
    Regards,
    Antonio

  7. #7
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Creating list based on 3 criteria

    Thats perfect. Thanks for your time and help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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