mercredi 25 février 2015

Finding a word in a range and inserting those cells that have the word into a column

I want to find a word in a range of cells and then copying the cells that have the word in another column.

for example the range of cells is



A
1 john see the man john
2 rain is the friend
3 john can teach
4 learn
5 learning those books
6 fred
7 continuing
8 learn john
9 see top
10 fresh verdic


I want to find the word "john" and copying those cells that contain john in column B:



B
1 john see the man john
2 john see the man john
3 john can teach
4 learn john


My code doesn't work.



Sub find()
Dim m_rnFind As Range
Dim m_stAddress As String

'Initialize the Excel objects.
Set m_wbBook = ThisWorkbook
Set m_wsSheet = m_wbBook.Worksheets("Book1")

Set m_rnCheck = m_wsSheet.Range("A1:A10").SpecialCells(xlCellTypeConstants)

'Retrieve all rows that contain john.
With m_rnCheck
Set m_rnFind = .find(What:="john")
i = 1
If Not m_rnFind Is Nothing Then
m_stAddress = m_rnFind.value
Cells(i, 2).Value = m_stAddress

Do

Set m_rnFind = .FindNext(m_rnFind)
i = i + 1
Loop While Not m_rnFind Is Nothing And m_rnFind.Address <> m_stAddress
End If
End With

End Sub

Aucun commentaire:

Enregistrer un commentaire