+ Reply to Thread
Results 1 to 6 of 6

VBA to search for multiple values in one sheet then copy to another sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    VBA to search for multiple values in one sheet then copy to another sheet

    Hi there
    Here’s the issue:

    I have three sheets….
    Sheet1 has the Data
    Sheet 2 is blank
    Sheet3 has the search Names {there are about 100 rows with name values}

    I want to search Sheet1 for all occurrences of where the name values from Sheet3 might occur, and then copy them to Sheet2.
    I have no idea where to start because I need the search to loop to search for the next name value from Sheet3.

    I have attached an example of the workbook layout; however there is no code.
    I know how to do a basic search and copy code, but I am unsure how this will work when the search value is held in 100+ rows on Sheet3.

    Here’s a little sample of what I am trying to do…….


    SHEET 1: (has many rows of data i need to search through)
    SHEET 2: (is currently blank, and is where the output from the search will go)
    SHEET 3: (has Coloum "A' filled with rows of names like DLPTPAYR or DLSYCNTY. These are table names, and I am searching a script from SHEET1 to see if these table names from SHEET3 appear on SHEET1 then copy them to SHEET2.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: VBA to search for multiple values in one sheet then copy to another sheet

    Sub compare()
    
    On Error Resume Next
    
    Set sh1 = Sheets("sheet1")
    Set sh2 = Sheets("sheet3")
    Set sh3 = Sheets("sheet2")
    
    r1max = sh1.Range("a" & Rows.Count).End(xlUp).Row
    
    r2 = 1
    r3 = 2
    
    While sh2.Cells(r2, 1) <> ""
      sk = sh2.Cells(r2, 1)
      For r1 = 1 To r1max
        j = 0
        j = WorksheetFunction.Find(sk, sh1.Cells(r1, 1))
        If j > 0 Then
          sh3.Cells(r3, 1) = j
          sh3.Cells(r3, 2) = sh1.Cells(r1, 1)
          r3 = r3 + 1
        End If
      Next
      r2 = r2 + 1
    Wend
    
    End Sub

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,595

    Re: VBA to search for multiple values in one sheet then copy to another sheet

    Try this
    Sub test()
        Dim a, i As Long, n As Long, myPtn As String
        myPtn = Join(Application.Transpose(Sheets("sheet3").Cells(1) _
        .CurrentRegion.Columns(1).Value), Chr(2))
        a = Sheets("sheet1").Cells(1).CurrentRegion.Value
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "([\$\(\)\-\^\|\\\{\}\[\]\+\*\?\.])"
            myPtn = Replace(.Replace(myPtn, "\$1"), Chr(2), "|")
            .Pattern = myPtn
            For i = 1 To UBound(a, 1)
                If .test(a(i, 1)) Then
                    n = n + 1: a(n, 1) = .Execute(a(i, 1))(0)
                End If
            Next
        End With
        If n > 0 Then Sheets("sheet2").Cells(1).Resize(n).Value = a
    End Sub

  4. #4
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: VBA to search for multiple values in one sheet then copy to another sheet

    I have now found that the data source is actually spread over multiple columns not just A.
    Is that an issue?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,595

    Re: VBA to search for multiple values in one sheet then copy to another sheet

    Quote Originally Posted by DanzaNZ View Post
    I have now found that the data source is actually spread over multiple columns not just A.
    Is that an issue?
    Sub test()
        Dim a, i As Long, ii As Long, myPtn As String, AL As Object
        Set AL = CreateObject("System.Collections.ArrayList")
        myPtn = Join(Application.Transpose(Sheets("sheet3").Cells(1) _
        .CurrentRegion.Columns(1).Value), Chr(2))
        a = Sheets("sheet1").UsedRange.Value
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "([\$\(\)\-\^\|\\\{\}\[\]\+\*\?\.])"
            myPtn = Replace(.Replace(myPtn, "\$1"), Chr(2), "|")
            .Pattern = myPtn
            For i = 1 To UBound(a, 1)
                For ii = 1 To UBound(a, 2)
                    If .test(a(i, ii)) Then
                        AL.Add .Execute(a(i, ii))(0)
                    End If
                Next
            Next
        End With
        If AL.Count Then Sheets("sheet2").Cells(1).Resize(AL.Count).Value = _
        Application.Transpose(AL.ToArray)
    End Sub

  6. #6
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: VBA to search for multiple values in one sheet then copy to another sheet

    Thank you Jindon
    Truly amazing help!
    Much appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Search a column within 1 sheet for text values if true copy that row to sheet 2
    By m77wls in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-19-2015, 05:32 PM
  2. Replies: 3
    Last Post: 03-31-2014, 01:00 PM
  3. Search word and copy line from multiple sheet to single sheet
    By drtorn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-23-2012, 01:35 AM
  4. [SOLVED] Looking for vba code that will search sheet for a value on a row and copy other values.
    By Justin25150 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-03-2012, 12:34 PM
  5. Search for Non-Duplicate Values Then Copy Row to Another Sheet
    By Stonesteel15 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-28-2012, 07:31 AM
  6. Search for Multiple String Values on Multiple Sheets and Copy Rows to New Sheet
    By rrtikker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-15-2012, 12:21 PM
  7. Search for multiple string values and copy row to new sheet
    By steeley7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2011, 04:00 AM

Tags for this Thread

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