Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Search column for string and write to cell

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Search column for string and write to cell

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

"Blee" wrote:

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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Search column for string and write to cell


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?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Search column for string and write to cell

On Mar 3, 4:37*pm, Blee wrote:
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?
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Search column for string and write to cell

On Mar 3, 5:03*pm, Blee wrote:
On Mar 3, 4:37*pm, Blee wrote:

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)


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Search column for string and write to cell

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

"Blee" wrote:

On Mar 3, 5:03 pm, Blee wrote:
On Mar 3, 4:37 pm, Blee wrote:

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)

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search text string for ssn and copy to new column mary Excel Discussion (Misc queries) 8 December 28th 07 03:13 AM
Search for a string, find the column and use that column to sort by [email protected] Excel Programming 3 October 12th 07 04:27 PM
Write/Paste RTF or HTML string into one cell?? Bourgui Excel Programming 3 June 29th 07 02:08 PM
URGENT -- search in string for a value in another column, if found, return value from next column samkshah Excel Programming 4 October 3rd 05 04:13 PM
Can not write string to a cell wmauss[_3_] Excel Programming 2 June 19th 04 08:56 AM


All times are GMT +1. The time now is 05:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"