+ Reply to Thread
Results 1 to 3 of 3

user defined macro to replace certain characters in a string based on user input

Hybrid View

  1. #1
    Registered User
    Join Date
    12-10-2012
    Location
    Boulder, CO
    MS-Off Ver
    Excel 2010
    Posts
    7

    user defined macro to replace certain characters in a string based on user input

    All,

    Declared... I am not a VBA programmer.

    Assume you want to replace a list of characters with another list of characters in a particular cell with a user defined function. For example... "!@#$%" and "* ()_" ... you want each instance of "!" to be replaced with "*", each instance of "@" with space, "#" with "(" and so on.

    Something like this for the active cell:
    function creplace()
        prompt user for from characters and to characters
        for x=1 to numcharacters
              cellvalue =  Replace(cellvalue, from_character, to_character)
        next x
    end function
    In other words, could somebody have the function invoke a dialogue the user for the "from characters" and the "to characters" so that this is truly dynamic.

    I ask because I have looked and not found this to my surprise. I was told ASAP utilities had it, but I installed it and could not find it.
    Last edited by vlady; 12-17-2012 at 07:46 PM. Reason: code tags

  2. #2
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: user defined macro to replace certain characters in a string based on user input

    dim st1 as string
    dim st2 as string
    st1 = inputbox("Find:","Title","Default")
    st2 = inputbox("Change to:","Title","Default")
    CELLVALUE = Replace(CELLVALUE,st1,st2)
    Last edited by GaidenFocus; 12-17-2012 at 06:08 PM.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: user defined macro to replace certain characters in a string based on user input

    Alter "SearchStr", "ReplStr" for your need.
    Sub test()
        MsgBox creplace("123_*mmd(sss)")
    End Sub
    
    Function creplace(ByVal txt As String) As String
        Dim i As Long
        Const SearchStr As String = "()*!@#$%_"
        Const ReplStr As String = "[]^* ()|+"
        For i = 1 To Len(SearchStr)
            txt = Replace(txt, Mid$(SearchStr, i, 1), Mid$(ReplStr, i, 1))
        Next
        creplace = txt
    End Function

+ 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.6.0 RC 1