Copy/Paste if cell equals a word

steven426

New Member
Joined
Jul 13, 2009
Messages
19
Hi:

I am trying to figure out how to go down a list of words and copy/paste a row if it equals a specific ring of words. For example, if want to go down a column and copy the word "Dog" if the word is "Dog" and paste it to the second sheet in a certain column... The thing that I can't figure out is how to step down to the next row until the cell is empty...

I tried the code below, which is nowhere close to working... thanks

Sub Cats_and_Dogs()
For i = 1 To 100
If Range("i, 1") = "Dog" Then
Range("i,1").Select
Selection.Copy
Sheet2.Paste
End If
Next
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Steven

Welcome to the Board

Code:
Sub Cats_and_Dogs()
Dim i, j As Long
For i = 1 To 100
j = Sheet2.Range("A" & Rows.Count).End(xlUp).Row + 1
If Sheet1.Cells(i, 1).Value = "Dog" Then
Sheet1.Cells(i, 1).Copy Sheet2.Range("A" & j)
End If
Next
End Sub
 
Upvote 0
Welcome to the board...

best way is to find the last used row ...

eg

Code:
Sub test()
For Each cell In Range(Cells(1, "A"), Cells(Cells(Rows.Count, "A").End(xlUp).Row, "A"))
    If cell.Value = "Dog" Then cell.Copy Destination:=Sheet2.Cells(Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 1, "A")
Next cell
End Sub
 
Upvote 0
Thanks a lot guys... This really helps... also, since my sheets have difference names than the ones in the example... do I need to define the sheet names somehow at the start of the code?

Thanks
 
Upvote 0
you could try something like this...then you just have to change Sheet1 and Sheet2 to the actual sheet names you are using

Code:
Sub test()
set cpySht=Sheets("Sheet1")
set pstSht=Sheets("Sheet2")
For Each cell In cpySht.Range(cpySht.Cells(1, "A"), cpySht.Cells(cpySht.Cells(Rows.Count, "A").End(xlUp).Row, "A"))
    If cell.Value = "Dog" Then cell.Copy Destination:=pstSht.Cells(pstSht.Cells(Rows.Count, "A").End(xlUp).Row + 1, "A")
Next cell
End Sub
 
Upvote 0
To clarify, is cpySht the actual name of the sheet (what I would see on the tab in the lower left corner of the screen) or is Sheet1 what would be written on the tab?

Thanks.
 
Upvote 0
cpySht is the variable that holds the name of the sheet....the actual sheet name in the example above is Sheet1...
 
Upvote 0
OK, so I've constructed this code from your posts, but it does not seem to be working.

Note, the sheet i am copying from is called Master Sheet and I am pasting to AGCY
 
Upvote 0
Sub Cats_and_Dogs()
Set cpySht = Sheets("Master List")
Set pstSht = Sheets("AGCY")
Dim i, j As Long
For i = 1 To 200
j = pstSht.Range("K" & Rows.Count).End(xlUp).Row + 1
If cpySht.Cells(i, 1).Value = "Dog" Then
cpySht.Cells(i, 1).Copy pstSht.Range("A" & j)
End If
Next
End Sub
 
Upvote 0
OK, so I've constructed this code from your posts, but it does not seem to be working.

Note, the sheet i am copying from is called Master Sheet and I am pasting to AGCY

where are you pasting the code?

To install code:
Press ALT-F11, Insert, Module
Paste the code from Sub ... End Sub into the blank area
 
Upvote 0

Forum statistics

Threads
1,215,736
Messages
6,126,550
Members
449,318
Latest member
Son Raphon

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