+ Reply to Thread
Results 1 to 5 of 5

Thread: Replace Words in a Textbox

  1. #1
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    180

    Replace Words in a Textbox

    I would like to create a function to take pieces of what a user types in a textbox and replace it with predefined alternatives. I only want it to change pieces and I want it to paste the alternative back into the same exact spot.

    Here is an example:

    if the user types "Black Beans" - I would like it to Replace the Black with BLK, so it would be like this, "Blk Beans"

    No matter what the other words that appear in the textbox - it should always change the word Black to Blk. There are other words as well that I want to do this with.

  2. #2
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,151

    Re: Replace Words in a Textbox

    When you say textbox, do you mean a textbox? Or a cell?

    Post a sample file with a textbox and a number of options you would want to replace and what to replace them with.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Replace Words in a Textbox

    Hello djblois1,

    The attached workbook contains a UserForm with a text box and command button. The substitution macro is run once the user has pressed the enter key. The command button closes the UserForm.

    "Sheet2" has two dynamic named ranges: WordList and SubList. These hold the words you want to change and the substitution text. Add on to these as needed. The searches are not case sensitive, so upper and lower case characters are treated the same.

    Here is the substitution macro code...
    'Written: October 25, 2010
    'Author:  Leith Ross (www.excelforum.com)
    
    Private Sub TextBox1_AfterUpdate()
    
      Dim I As Long
      Dim RegExp As Object
      Dim SubList As Variant
      Dim WordList As Variant
      Dim X As String
      
        WordList = WorksheetFunction.Transpose(Range("WordList"))
        SubList = WorksheetFunction.Transpose(Range("SubList"))
        
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.IgnoreCase = True
        
          For I = 1 To UBound(WordList)
            RegExp.IgnoreCase = True
            RegExp.Pattern = "(\b" & WordList(I) & "\b)(.*)"
            
            Do While RegExp.Test(TextBox1.Value)
              X = RegExp.Execute(TextBox1.Text)(0).SubMatches(0)
              TextBox1.Value = RegExp.Replace(TextBox1, SubList(I) & "$2")
             'Test to prevent endless loop
              If SubList(I) = X Then
                 RegExp.IgnoreCase = False
              Else
                 RegExp.IgnoreCase = True
              End If
            Loop
            
          Next I
          
        Set RegExp = Nothing
       
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Replace Words in a Textbox

    Leith,

    I will work on this a little later today but will this work if my Textbox is on a form?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Replace Words in a Textbox

    Hello djblois1,

    The text box in the workbook example I posted is on a VBA User Form. This is the same as a Control Toolbox (ActiveX) Text Box. Control Toolbox controls can be placed on either worksheets or VBA User Forms. You will need this type of control to make this work because it driven by the AfterUpdate event. This code can not be used with a Forms control, which can only be used on worksheets.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0