How to replace multiple words from the text box?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Govind Pandey

    How to replace multiple words from the text box?

    Hi,

    I am trying to replace multiple words from the text box.


    for example:
    I want few words should be in Upper case only. Like "cat" to be replaced with "CAT", "dog" always appears as "DOG", "bat" to be replaced with "BAT" and "Hub" always appears as "HUB",

    I put following code for the command button click event:


    [code]
    Private Sub cmdreplace_Clic k()

    Dim txt

    txt = Text1.Text

    text1.text = Replace(txt, " cat ", " CAT ").Replace( txt, " dog ", " DOG ").Replace( txt, " rat ", " RAT ").Replace( txt, " hub ", " HUB ")

    End Sub
    [code]

    This is not working. Any idea ...
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    You can only do one replacement at a time.

    Code:
    Private Sub Command1_Click()
    Text1 = Replace(Text1, "cat", "CAT")
    Text1 = Replace(Text1, "dog", "DOG")
    Text1 = Replace(Text1, "rat", "RAT")
    End Sub

    Comment

    • Rodney Roe
      New Member
      • Oct 2010
      • 61

      #3
      You can also do it this way,

      Code:
      text1 = Replace(Replace(Replace(Replace(txt, "cat", "CAT"), "dog", "DOG"), "rat", "RAT"), "hub", "HUB")

      Comment

      Working...