Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Search column for string and write to cell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Blee - 03 Mar 2008 22:52 GMT
Not sure why I cannot find any code to do this, but.... I'm looking to
write a macro that would search a specific column for a string
(contains) and write a specific value to another cell in that same row
if there is a match. I.e., If column Z has a string that contains
"zebra", write "animal" to cell B of that same row. Anyone?
Tom Hutchins - 03 Mar 2008 23:49 GMT
One example...

Paste this code in a VBA module in your workbook. Click any cell in the
column to be searched, then run the macro (Tools >> Macro >> Macros >>
FindText >> Run).

Sub FindText()
Dim c As Range, ret
Const FindThis = "zebra"
Const WriteThis = "animal"
ActiveCell.EntireColumn.Select
For Each c In Selection
   ret = InStr(c, FindThis)
   If (Not IsNull(ret)) And (ret > 0) Then
       c.Offset(0, 1).Value = WriteThis
   End If
Next c
End Sub

Hope this helps,

Hutch

> Not sure why I cannot find any code to do this, but.... I'm looking to
> write a macro that would search a specific column for a string
> (contains) and write a specific value to another cell in that same row
> if there is a match. I.e., If column Z has a string that contains
> "zebra", write "animal" to cell B of that same row. Anyone?
Blee - 04 Mar 2008 00:37 GMT
Thank you, Tom. Very interesting. And I can control which cell is
written to by using the Offset +/- values. Any way to actually specify
the cell by column letter?
Blee - 04 Mar 2008 01:03 GMT
> Thank you, Tom. Very interesting. And I can control which cell is
> written to by using the Offset +/- values. Any way to actually specify
> the cell by column letter?

Also, is there a flag for case insensitive searches?
Blee - 04 Mar 2008 01:15 GMT
> > Thank you, Tom. Very interesting. And I can control which cell is
> > written to by using the Offset +/- values. Any way to actually specify
> > the cell by column letter?
>
> Also, is there a flag for case insensitive searches?

   ret = InStr(1, c, FindThis, vbTextCompare)
Tom Hutchins - 04 Mar 2008 14:17 GMT
Here are two more versions of the same subroutine. The first does a
case-sensitive search, and both the column to search and the column to which
to write are specified as constants. The second version prompts the user for
everything.

Sub FindText1()
Dim c As Range, ret
Const FindThis = "zebra"
Const WriteThis = "animal"
Const SearchCol = "A"
Const WriteCol = "B"
Range(SearchCol & "1").EntireColumn.Select
For Each c In Selection
   ret = InStr(1, c, FindThis, 0)
   If (Not IsNull(ret)) And (ret > 0) Then
       Range(WriteCol & c.Row).Value = WriteThis
   End If
Next c
End Sub

Sub FindText2()
Dim c As Range, FindThis As String, CaseSens As Integer
Dim WriteThis As String, SearchCol As String
Dim WriteCol As String, ret
FindThis = InputBox("Text to find", "FindText2", "zebra")
If Len(FindThis) = 0 Then Exit Sub
WriteThis = InputBox("Text to write", "FindText2", "animal")
If Len(WriteThis) = 0 Then Exit Sub
SearchCol = InputBox("Search which column?", "FindText2", "A")
If Len(SearchCol) = 0 Then Exit Sub
WriteCol = InputBox("Write to which column?", "FindText2", "B")
If Len(WriteCol) = 0 Then Exit Sub
ret = MsgBox("Case-sensitive?", vbYesNo, "FindText2")
If ret = vbYes Then
   CaseSens = 0
Else
   CaseSens = 1
End If
Range(SearchCol & "1").EntireColumn.Select
For Each c In Selection
   ret = InStr(1, c, FindThis, CaseSens)
   If (Not IsNull(ret)) And (ret > 0) Then
       Range(WriteCol & c.Row).Value = WriteThis
   End If
Next c
End Sub

Hope this helps,

Hutch

> > > Thank you, Tom. Very interesting. And I can control which cell is
> > > written to by using the Offset +/- values. Any way to actually specify
[quoted text clipped - 3 lines]
>
>     ret = InStr(1, c, FindThis, vbTextCompare)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.