+ Reply to Thread
Results 1 to 5 of 5

Search and copy based on partial text match

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2014
    MS-Off Ver
    Excel 2003
    Posts
    3

    Search and copy based on partial text match

    Hi everyone!

    I have a list in Column A with 1000 Rows, all are texts. In D1 and E1 I have two texts, which would be the search criterias.
    I'd like to search the Column A, if the values in D1 and E1 are part of a row simultaneously but it can be only partial match. I cannot use VLOOKUP because it can give multiple results. The results should be listed in a separate sheet. Here is an example:

    Column A
    ABD_DDT_VitaminB12
    ABD_dka_VitaminB2
    ABD_dka_VitaminB2_version2
    DAT_kft_VItaminB2

    And I'd like to see in a separate sheet only the rows which contains "ABD" and "VitaminB2"

    Thank you!

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Search and copy based on partial text match


    Hi,

    can be done with an advanced filter with or whitout any code …

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Search and copy based on partial text match

    Try this:

    Sub RunMe()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet2")
    Dim LR As Long, LC As Long
    Dim rCell As Range, rSearch As Range, str1Find As Range, str2Find As Range
    Dim str1 As String, str2 As String
    
    LR = ws1.Range("A" & Rows.Count).End(xlUp).Row
    LC = ws1.UsedRange.Columns.Count
    
    str1 = ws1.Range("D1").Value
    str2 = ws1.Range("E1").Value
    
    For Each rCell In ws1.Range("A2:A" & LR)
        Set rSearch = rCell.Resize(1, LC + 1)
        Set str1Find = rSearch.Find(What:=str1, LookIn:=xlValues, Lookat:=xlPart)
        Set str2Find = rSearch.Find(What:=str2, LookIn:=xlValues, Lookat:=xlPart)
        If Not str2Find Is Nothing And Not str1Find Is Nothing Then
            rCell.EntireRow.Copy Destination:=ws2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
    Next rCell
    
    End Sub

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search and copy based on partial text match

    Try on a copy 1st. Copies to Sheet2.

    Sub PIQalap()
    Dim rcell As Range
    Columns(2).Insert
    Columns(2).Insert
    Range("B2:B" & Range("A" & Rows.count).End(3)(1).Row).Formula = "=VLOOKUP(""*"" & $F$1 & ""*"",$A2:$A2,1,FALSE)"
    Range("C2:C" & Range("A" & Rows.count).End(3)(1).Row).Formula = "=VLOOKUP(""*"" & $G$1 & ""*"",$A2:$A2,1,FALSE)"
    With Range("B2:C" & Range("A" & Rows.count).End(3)(1).Row)
        .Value = .Value
        .Replace "#N/A", ""
    End With
    Exit Sub
    For Each rcell In Range("B2:B" & Range("A" & Rows.count).End(3)(1).Row)
        If rcell.Value <> "" And rcell.offset(, 1).Value <> "" Then
            rcell.EntireRow.Copy Sheets("Sheet2").Range("A" & Rows.count).End(3)(2)
        ElseIf rcell.offset(, 1).Value <> "" And rcell.Value <> "" Then
            rcell.EntireRow.Copy Sheets("Sheet2").Range("A" & Rows.count).End(3)(2)
        End If
    Next rcell
    Columns("B:C").Delete
    End Sub

  5. #5
    Registered User
    Join Date
    05-12-2014
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Search and copy based on partial text match

    Thanks! The first one works perfectly! You made my day :D

+ 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. Replies: 0
    Last Post: 09-21-2013, 09:03 PM
  2. Copy column based on partial word match in first row and paste into existing workbook
    By David Harris 1987 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-19-2013, 03:52 PM
  3. Replies: 3
    Last Post: 07-10-2013, 01:39 PM
  4. if match partial text from a cell to a range copy row
    By Shansy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-21-2013, 02:35 AM
  5. remove duplicates based on partial text match
    By smelkin in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-16-2011, 09:45 AM

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