VBA Code to look for a Value in a Column and Copy entire row to another sheet.

zerlotus

New Member
Joined
Jul 3, 2017
Messages
15
Hello everyone,

I'm looking for a simple code that:

Based on a cell value in Sheet 1 (A21), it looks for it in Sheet 2 column A. Then it copies the entire row and paste in A2 Sheet 3.

I tried several ways, look for guidance in the forum/google, but could not find a thing that i could use. They all were to complex as to me being able to simplify it.

Could someone help me? :)

Thank you.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Here is an untested (you did not provide sample data to test) VBA module.

VBA Code:
Option Explicit

Sub Zerlotus()
    Dim s1 As Worksheet, s2 As Worksheet, s3 As Worksheet
    Dim crit, lr2 As Long, lr3 As Long
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Set s3 = Sheets("Sheet3")
    crit = s1.Range("A21")
    lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
    Dim i As Long
    For i = 2 To lr2    'assumes row 1 has a header. Start search in row 2
        If s2.Range("A" & i) = crit Then
            s2.Range("A" & i).EntireRow.Copy
            s3.Range("A2").PasteSpecial xlPasteValues
        End If
    Next i
    Application.CutCopyMode = False
    MsgBox "Found"
End Sub
 
Upvote 0
Assuming your data like this:
Dante Amor
AB
1
21some
22
Sheet1

Dante Amor
ABCD
1abcd
2someb2c2d2
3a3b3c3d3
4a4b4c4d4
5someb5c5d5
6a6b6c6d6
7someb7c7d7
8a8b8c8d8
9someb9c9d9
10a10b10c10d10
Sheet2

Results:
Dante Amor
ABCDE
1
2someb2c2d2
3someb5c5d5
4someb7c7d7
5someb9c9d9
6
Sheet3



How about:
VBA Code:
Sub Copy_Rows()
  With Sheets("Sheet2")
    .Range("A1").AutoFilter 1, Sheets("Sheet1").Range("A21")
    .AutoFilter.Range.Offset(1).EntireRow.Copy Sheets("Sheet3").Range("A2")
    .ShowAllData
  End With
End Sub
 
Upvote 0
Hello alansidman & DanteAmor,

Both of your solutions worked effortleslly great.

Thank you both, I found the two solutions really useful. :)
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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