Making the contents of the active cell the search term, and search using that term, copy paste row, next

Travis Kunnen

New Member
Joined
Feb 24, 2016
Messages
21
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a large Excel data base of bacteria Genus'and i need to match the name in one worksheet, to another worksheet, copy that whole row, and paste into another worsksheet.
Example... I have 3 worksheets A, B, C

Worksheet A has 1000's of rows of names and many columns of data to each row name, but the name is always in Column A and will be alphabetical.
Worksheet B has the pasted names in Column A (also alphabetical) im looking to match in Worksheet A, column A.
Worksheet C, is for pasting. or can pasted to the right of the name in Worksheet B.

So... open worksheet B, cell A1.. that word there "ABAB", use this cell as the search term to search in worksheet A, cell A1... if a match... copy all of worksheet A Row 1, and paste into worksheet C Row 1.
Repeat, open worksheet B, cell A2... that word there, "GHGH, use this cell as the search term to search in worksheet A, cell A2 = negative, cell A3 = negative, cell A4 match. Copy entire row A4, paste into worksheet C row B..... Continue until blank empty cell in worksheet B, Column A.
So the search word / term will be different for each search, and terms shouldnt be repeated, cause it will be alphabetical.
Right now im doing this manually for 1000's of rows of data.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Starting with:

Book1
ABCDE
1Name
2A4142319
3B582110
4C1610518
5D19212322
6E157115
7F2025212
8G113228
9H23111216
10I2323715
11J31085
12K2101811
13L19172321
14M54316
15N1541911
Sheet2

And a lookup sheet like:

Book1
A
1Name
2C
3F
4G
5X
6A
Sheet3


We can get to this:

Book1
ABCDE
1
2C1610518
3F2025212
4G113228
5XNot found
6A4142319
Sheet4


With this macro:

VBA Code:
Sub FastLookup()
Dim WS1 As Worksheet, WS2 As Worksheet, WS3 As Worksheet
Dim NumCols As Long, MySrc As Variant, MyDat As Variant
Dim MyOut As Variant, r As Long, r2 As Long, c As Long

    Set WS1 = Worksheets("Sheet2")
    Set WS2 = Worksheets("Sheet3")
    Set WS3 = Worksheets("Sheet4")
    
    NumCols = 5
    
    MySrc = WS1.Range("A2", WS1.Range("A1000000").End(xlUp).Offset(, NumCols - 1))
    MyDat = WS2.Range("A2", WS2.Range("A1000000").End(xlUp))
    
    ReDim MyOut(1 To UBound(MyDat), 1 To NumCols)
    
    For r = 1 To UBound(MyDat)
        r2 = 0
        On Error Resume Next
        r2 = WorksheetFunction.Match(MyDat(r, 1), WorksheetFunction.Index(MySrc, 0, 1), 0)
        If r2 = 0 Then
            MyOut(r, 1) = MyDat(r, 1)
            MyOut(r, 2) = "Not found"
        Else
            For c = 1 To NumCols
                MyOut(r, c) = MySrc(r2, c)
            Next c
        End If
    Next r
    
    WS3.Cells.ClearContents
    WS3.Range("A2").Resize(UBound(MyDat), NumCols) = MyOut
        
End Sub


Change the worksheet names and the NumCols value at the beginning to match your sheet. Let me know if you need help installing it or using it.
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top