+ Reply to Thread
Results 1 to 3 of 3

Searching multiple columns

Hybrid View

Erenagh Searching multiple columns 06-16-2011, 02:38 PM
tigeravatar Re: Searching multiple columns 06-16-2011, 03:28 PM
Erenagh Re: Searching multiple columns 06-17-2011, 01:31 PM
  1. #1
    Registered User
    Join Date
    06-01-2011
    Location
    Dayton, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    20

    Searching multiple columns

    I want to develop a macro that will allow me to search multiple columns and then copy the respective row onto another sheet

    Search 1:
    Column A = "y"
    Column B = "" <~Blank

    Copy to sheet 2

    Search 2:
    Column A = "y"
    Column C = "y"

    Copy to sheet 2

    I have code the I used for 1 search term that works great from tigeravatar:
        Dim ws1 As Worksheet: Set ws1 = Sheets("Sheet1")
        Dim ws2 As Worksheet: Set ws2 = Sheets("Sheet2")
       
        Dim ws2DestRow As Long: ws2DestRow = 1
    
        Dim z As Long
    
        For z = 4 To ws1.Cells(Rows.Count, "A").End(xlUp).Row
            If Trim(LCase(ws1.Cells(z, "A").Value)) = "y" Then
                ws2.Range("C" & ws2DestRow).EntireRow.Value = ws1.Cells(z, "A").EntireRow.Value
                ws2DestRow = ws2DestRow + 1
            End If
        Next
    Last edited by Erenagh; 06-17-2011 at 01:31 PM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Searching multiple columns

    Erenagh,

    Could give this a try:
        Dim ws1 As Worksheet: Set ws1 = Sheets("Sheet1")
        Dim ws2 As Worksheet: Set ws2 = Sheets("Sheet2")
       
        Dim ws2DestRow As Long: ws2DestRow = 1
        Dim z As Long
        
        For z = 4 To ws1.Cells(Rows.Count, "A").End(xlUp).Row
            If Trim(LCase(ws1.Cells(z, "A").Value)) = "y" _
            And Trim(ws1.Cells(z, "B").Value) = vbNullString Then
                ws2.Range("C" & ws2DestRow).EntireRow.Value = ws1.Cells(z, "A").EntireRow.Value
                ws2DestRow = ws2DestRow + 1
            ElseIf Trim(LCase(ws1.Cells(z, "A").Value)) = "y" _
            And Trim(LCase(ws1.Cells(z, "C").Value)) = "y" Then
                ws2.Range("C" & ws2DestRow).EntireRow.Value = ws1.Cells(z, "A").EntireRow.Value
                ws2DestRow = ws2DestRow + 1
            End If
        Next


    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    06-01-2011
    Location
    Dayton, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    20

    Smile Re: Searching multiple columns

    Amazing as always Tiger.

+ 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