View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Gary''s Student Gary''s Student is offline
external usenet poster
 
Posts: 11,058
Default Find & replace string in text

This is coded for cell A1:

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then
Exit Sub
End If
Set r = Target
Dim s As String
s = r.Value
s = Replace(s, "x", "[")
s = Replace(s, "X", "[")
s = Replace(s, "z", "]")
s = Replace(s, "Z", "]")
Application.EnableEvents = False
r.Value = s
Application.EnableEvents = True
End Sub

--
Gary''s Student - gsnu200738


"BeSmart" wrote:

Hi
I want to create a worksheet_change event that searchs for a string within
text (as it's typed into a cell or after a spacebar or return is entered),
and replaces the specific string found with a different character or symbol -
without affecting the other text in the cell.

e.g. In any cell on a worksheet, a user types "X-ray and Zebra", I need
excel to find all the "x"s and replace them with "[" plus find and replace
any "z"s with "]".

Not sure if this possible? If anyone can help me to create this I'd really
appreciate it.

P.S. I'm a novice at VBA, and I'm trying to teach myself how to use VBA by
dissecting examples I find and learn about on the web, so sorry if I haven't
provided good information.....
--
Thank for your help
BeSmart