Copy & Paste Rows that Contain Specific Text

fogdogzz

New Member
Joined
Oct 17, 2013
Messages
11
I have used the following code to copy and paste rows that have values in Column "C" that are greater than the value of cell W62. I like the structure of it because it runs more quickly than some other macro options. However, I am having a difficult time adjusting it to copy rows that contain the word "Sales" in Column "D". [Note the individual cell might say "Sales1" or "SalesBC", not just "Sales"- so the contains aspect is key]. I am hoping that someone could add some insight.

Thank you very much!

Current Macro:

Dim rws1 As Range, cell1 As Range, value As Long

Sheets("Sheet1").Select
Set cell1 = Range("c2")
Do Until cell1.value = ""
value = Val(cell1.value)
If (value < Sheets("Sheet1").Range("w62").value) Then
If rws1 Is Nothing Then
Set rws1 = cell1.EntireRow
Else
Set rws1 = Union(cell1.EntireRow, rws1)
End If
End If
Set cell1 = cell1.Offset(1)
Loop
If Not rws1 Is Nothing Then rws1.Copy
Sheets("Sheet2").Range("A2").PasteSpecial xlPasteValues

Set rws1 = Nothing

Set cell1 = Nothing

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
818
Office Version
  1. 365
Platform
  1. Windows
Is this a specific code you want to use on Col D only or do you want to loop thru Col C at the same time, using your rules above?

FarmerScott
 
Upvote 0

fogdogzz

New Member
Joined
Oct 17, 2013
Messages
11
Good question! I should have been more clear. I am looking for separate code- just for Col D (and the contains "Sales" parameters).

Thanks, Scott.

Is this a specific code you want to use on Col D only or do you want to loop thru Col C at the same time, using your rules above?

FarmerScott
 
Upvote 0

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
818
Office Version
  1. 365
Platform
  1. Windows
FogDogzz,

try-

Code:
Sub find_and_copy()
Dim rws1 As Range, cell1 As Range, value As Long, lastrow As Long

Sheets("Sheet1").Activate
Set cell1 = Sheets("Sheet1").Range("D2")
Set fval = cell1.Find("Sales", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)

Do Until cell1.value = ""
If cell1.value = fval Then

lastrow = Sheets("Sheet2").Cells.SpecialCells(xlLastCell).Row + 1
Set rws1 = cell1.EntireRow
rws1.Copy
Sheets("Sheet2").Range("A" & lastrow).PasteSpecial xlPasteValues
End If
Loop
End Sub

Hope that helps,

FarmerScott
 
Upvote 0

Forum statistics

Threads
1,191,166
Messages
5,985,051
Members
439,935
Latest member
Monty238

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
Top