VBA Search Engine Only Carries Over One Cell Instead Of The Cell's Entire Row

PeerGuy

New Member
Joined
Sep 12, 2017
Messages
10
Greetings,

Currently I’m struggling to create a search engine in vba that compares user ID’s on two different sheets (sheet1 & sheet2) and then returns the results of the search onto sheet3. [FONT=arial, sans-serif]So basically vba searches through Sheet2 to see if it has any matching values to Sheet1 in this case the VBA is searching for matching user IDs. Column A of Sheet1 specifically starting at A4 iand s being compared to Sheet2's Column L that starts at L4. Once a match is found between the columns of both sheets the entire row of the matched cell in sheet2 is supposed to be carried over onto sheet3 within the range of k5 to v5 and continue to k6 to v6, k7 to v7 etc. However, the current code only copies the one single matched cell and not the matched cell's entire row into the desired range of K5 to V5 in Sheet3. Is there a way to fix this? My code is listed below. Any suggestions would be greatly appreciated. Thank you, first time here but I've always heard great things. Appreciate any help that's given.[/FONT]

Code:
Option Explicit
 
Sub FindWhat()
 
Dim sFindWhat As String
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim sh3 As Worksheet
Dim Search As Range
Dim Addr As String
Dim NextRow As Long
Dim cl As Range
 
Set sh1 = ThisWorkbook.Sheets("Sheet1")
Set sh2 = ThisWorkbook.Sheets("Sheet2")
Set sh3 = ThisWorkbook.Sheets("Sheet3")
 
'// This will be the row you start pasting data on Sheet3
NextRow = 5
 
For Each cl In Intersect(sh1.UsedRange, sh1.Columns("A")).Cells
    '// the value we're looking for
    sFindWhat = cl.Value
    '// Find this value in Sheet2:
    With sh2.UsedRange
        Set Search = .Find(sFindWhat, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext)
        If Search Is Nothing Then
            '// Get out of here if the value is not found
            '// Do NOT Exit the sub, we'll just proceed to next cell in column A
            'Exit Sub
        Else
            '// Make sure next row in Sh3.Column("K") is empty
            While sh3.Range("K" & NextRow).Value <> ""
                NextRow = NextRow + 1
            Wend
            '// Paste the row in column K of sheet 3:
            Search.Resize(1, 12).Copy Destination:=sh3.Range("K" & NextRow)
        End If
   End With
Next
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I tested your code in post 6. Correcting the mistakes (see below) it worked perfectly for me.

Code:
Sub FindWhat()
    Dim sFindWhat As String
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim sh3 As Worksheet
    Dim Search As Range
    Dim Addr As String
    Dim NextRow As Long
    Dim cl As Range
    
    Set sh1 = ThisWorkbook.Sheets("Sheet1")
    Set sh2 = ThisWorkbook.Sheets("Sheet2")
    Set sh3 = ThisWorkbook.Sheets("Sheet3")
    
    '// This will be the row you start pasting data on Sheet3
    NextRow = 5
    
    For Each cl In Intersect(sh1.UsedRange, sh1.Columns("A")).Cells
        '// the value we're looking for
        sFindWhat = cl.Value
        '// Find this value in Sheet2:
        With sh2.Columns(12)
            Set Search = .Find(sFindWhat, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, LookAt:=xlWhole)
            If Search Is Nothing Then
                'Do nothing
            Else
                '// Make sure next row in Sh3.Column("K") is empty
                While sh3.Range("K" & NextRow).Value <> ""
                    NextRow = NextRow + 1
                Wend
                '// Paste the row in column K of sheet 3:
                Range(Search.EntireRow.Cells(1, "A"), Search).Copy Destination:=sh3.Range("K" & NextRow)
            End If
        End With
    Next
End Sub

Test data

Sheet1

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD="bgcolor: #DCE6F1"]
ID1​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD="bgcolor: #DCE6F1"]
ID2​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD="bgcolor: #DCE6F1"]
ID3​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD="bgcolor: #DCE6F1"]
ID7​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD="bgcolor: #DCE6F1"]
ID10​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Sheet2

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[TD="bgcolor: #DCE6F1"]
F
[/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[TD="bgcolor: #DCE6F1"]
H
[/TD]
[TD="bgcolor: #DCE6F1"]
I
[/TD]
[TD="bgcolor: #DCE6F1"]
J
[/TD]
[TD="bgcolor: #DCE6F1"]
K
[/TD]
[TD="bgcolor: #DCE6F1"]
L
[/TD]
[TD="bgcolor: #DCE6F1"]
M
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
Data1​
[/TD]
[TD]
Data10​
[/TD]
[TD]
Data19​
[/TD]
[TD]
Data28​
[/TD]
[TD]
Data37​
[/TD]
[TD]
Data46​
[/TD]
[TD]
Data55​
[/TD]
[TD]
Data64​
[/TD]
[TD]
Data73​
[/TD]
[TD]
Data82​
[/TD]
[TD]
Data91​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID1​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
Data2​
[/TD]
[TD]
Data11​
[/TD]
[TD]
Data20​
[/TD]
[TD]
Data29​
[/TD]
[TD]
Data38​
[/TD]
[TD]
Data47​
[/TD]
[TD]
Data56​
[/TD]
[TD]
Data65​
[/TD]
[TD]
Data74​
[/TD]
[TD]
Data83​
[/TD]
[TD]
Data92​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID2​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
Data3​
[/TD]
[TD]
Data12​
[/TD]
[TD]
Data21​
[/TD]
[TD]
Data30​
[/TD]
[TD]
Data39​
[/TD]
[TD]
Data48​
[/TD]
[TD]
Data57​
[/TD]
[TD]
Data66​
[/TD]
[TD]
Data75​
[/TD]
[TD]
Data84​
[/TD]
[TD]
Data93​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID3​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
Data4​
[/TD]
[TD]
Data13​
[/TD]
[TD]
Data22​
[/TD]
[TD]
Data31​
[/TD]
[TD]
Data40​
[/TD]
[TD]
Data49​
[/TD]
[TD]
Data58​
[/TD]
[TD]
Data67​
[/TD]
[TD]
Data76​
[/TD]
[TD]
Data85​
[/TD]
[TD]
Data94​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID4​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Result in Sheet3

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
K
[/TD]
[TD="bgcolor: #DCE6F1"]
L
[/TD]
[TD="bgcolor: #DCE6F1"]
M
[/TD]
[TD="bgcolor: #DCE6F1"]
N
[/TD]
[TD="bgcolor: #DCE6F1"]
O
[/TD]
[TD="bgcolor: #DCE6F1"]
P
[/TD]
[TD="bgcolor: #DCE6F1"]
Q
[/TD]
[TD="bgcolor: #DCE6F1"]
R
[/TD]
[TD="bgcolor: #DCE6F1"]
S
[/TD]
[TD="bgcolor: #DCE6F1"]
T
[/TD]
[TD="bgcolor: #DCE6F1"]
U
[/TD]
[TD="bgcolor: #DCE6F1"]
V
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
Data1​
[/TD]
[TD]
Data10​
[/TD]
[TD]
Data19​
[/TD]
[TD]
Data28​
[/TD]
[TD]
Data37​
[/TD]
[TD]
Data46​
[/TD]
[TD]
Data55​
[/TD]
[TD]
Data64​
[/TD]
[TD]
Data73​
[/TD]
[TD]
Data82​
[/TD]
[TD]
Data91​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID1​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
Data2​
[/TD]
[TD]
Data11​
[/TD]
[TD]
Data20​
[/TD]
[TD]
Data29​
[/TD]
[TD]
Data38​
[/TD]
[TD]
Data47​
[/TD]
[TD]
Data56​
[/TD]
[TD]
Data65​
[/TD]
[TD]
Data74​
[/TD]
[TD]
Data83​
[/TD]
[TD]
Data92​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID2​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
Data3​
[/TD]
[TD]
Data12​
[/TD]
[TD]
Data21​
[/TD]
[TD]
Data30​
[/TD]
[TD]
Data39​
[/TD]
[TD]
Data48​
[/TD]
[TD]
Data57​
[/TD]
[TD]
Data66​
[/TD]
[TD]
Data75​
[/TD]
[TD]
Data84​
[/TD]
[TD]
Data93​
[/TD]
[TD="bgcolor: #D9D9D9"]
ID3​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


M.
 
Upvote 0
Ok so I literally copy and pasted the code that you just posted and now I have an error in the "If Search Is Nothing Then" and the "End If" line of code at the bottom. I don't know why this is happening, perhaps it's my work computer? When I tried the original code on my home computer it worked fine but now it seems to keep giving me these errors that never came up before. This is extremely confusing, I do appreciate your help though. Excel just keeps saying code interrupted and highlights the "If Search Is Nothing Then" and the "End If" lines of code. Perhaps there's a way to bypass these errors? If it makes a difference at home I have the latest version of Excel but at work I'm using Excel 2010 if that makes a difference.
 
Upvote 0
So I guess what I'm asking is if there's a way to ignore the code execution has been interrupted error? That's what I'm trying to figure it out.
 
Upvote 0
I figured it out it's a glitch in this version of Excel as in Excel 2010. Application.EnableCancelKey=xlDisabled you put this in the first line of code gets rid of the whole problem! Thank you for everything, I'm trying to quietly not curse at Microsoft right now!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
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