PDA

View Full Version : Copy & paste based on value



congokin44
03-25-2010, 09:53 PM
i have a list of words in colA of sheet1. i want a macro to copy every row that have the string "red" and paste it to colA in sheet2. ColA can have more than 1 word per cell

lucas
03-25-2010, 10:04 PM
Maybe this will work:


Option Explicit
Sub FindCodes()
Dim rngToSearch As Range
Dim cel1 As Range
Dim c As Range
Dim counter As Integer
Dim firstAddress As String
Dim MyInput As String
Sheets("Output Sheet").UsedRange.ClearContents
counter = 1 'start output in row 1
MyInput = InputBox("Search for String...", "Search", "Enter your search sting here")
Set rngToSearch = Sheets("Sheet2").Columns(1)
Set c = rngToSearch.Find(What:=MyInput, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.EntireRow.Copy Worksheets("Output Sheet").Rows(counter)
counter = counter + 1
Set c = rngToSearch.FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
Sheets("Output Sheet").Select
End Sub

example attached

congokin44
03-25-2010, 10:43 PM
this works perfect but is it possible to modify the macro so that i can hardcode the keyword in the macro instead of being on a input box

lucas
03-26-2010, 06:19 AM
sure, just change this line:
MyInput = InputBox("Search for String...", "Search", "Enter your search sting here")


to something like this:


MyInput = "xxxx"


where xxxx is the string you want to search for.

congokin44
03-26-2010, 01:07 PM
thanks this works perfect