Replace help

Posted on 2005-04-29
Last Modified: 2010-05-02
Hi how can I replace multiple strings instead of this:::
RT.Text = Replace(RT.Text, "dim", "Dim")
RT.Text = Replace(RT.Text, "as", "As")
RT.Text = Replace(RT.Text, "integer", "Integer")
RT.Text = Replace(RT.Text, "string", "String")
RT.Text = Replace(RT.Text, "variant", "Variant")
RT.Text = Replace(RT.Text, "long", "Long")
RT.Text = Replace(RT.Text, "boolean", "Boolean")
RT.Text = Replace(RT.Text, "byte", "Byte")
RT.Text = Replace(RT.Text, "currency", "Currency")
RT.Text = Replace(RT.Text, "private sub", "Private Sub")

Is there an easier way to replace these strings instead of having to keep writing them line by line like above code
Question by:nffvrxqgrcfqvvc
    LVL 11

    Expert Comment

    this looks like a job for a text editor
    LVL 12

    Expert Comment

    Do you have source code in a textbox, and are trying to do some cleanup on the code?

    LVL 52

    Assisted Solution

    by:Carl Tawn
    Hmm, you could put all the combinations into an array and loop through them doing the replace.

    Something like:

    Dim arrVals(2, 10) As String

    Private Sub Form_Load()

        arrVals(0, 0) = "dim": arrVals(1, 0) = "Dim"
        arrVals(0, 1) = "as": arrVals(1, 1) = "As"
        arrVals(0, 2) = "integer": arrVals(1, 2) = "Integer"
        arrVals(0, 3) = "string": arrVals(1, 3) = "String"
        arrVals(0, 4) = "variant": arrVals(1, 4) = "Variant"
        arrVals(0, 5) = "long": arrVals(1, 5) = "Long"
        arrVals(0, 6) = "boolean": arrVals(1, 6) = "Boolean"
        arrVals(0, 7) = "byte": arrVals(1, 7) = "Byte"
        arrVals(0, 8) = "currency": arrVals(1, 8) = "Currency"
        arrVals(0, 9) = "private sub": arrVals(1, 9) = "Private Sub"

    End Sub

    Private Sub cmdReplace_Click()

        Dim i As Integer

        For i = 0 To UBound(arrVals, 2) - 1
            RT.Text = Replace(RT.Text, arrVals(0, i), arrVals(1, i))
        Next i

    End Sub

    Then all you have to do is increase the size of the array to add any other keywords you want to replace on.

    Hope this helps.
    LVL 29

    Author Comment

    Sorry but the array is just as much coding as using the you notice that all I am doing is change lowercase to capitital case...

    dim = Dim

    I am using a richtextbox...Im just creating a vidual basic style code the special characters are the keyword color which is blue

    So if someone type in   dim test as string it would replace it like the vb source code format

    Dim = blue color test=black As=blue color String=blue color
    Dim test As String

    I am just trying to strip all the replace code down so i dont have to keep adding it like the code I posted..I had an idea to just capitilaze each word after the im experimenting
    LVL 29

    Author Comment

    'nevermind I got it to work

     Dim nPos As Integer
        nPos = RT.SelStart
        RT.Text = StrConv(RT.Text, vbProperCase)
        RT.SelStart = nPos
    LVL 12

    Expert Comment

    Hey, that is a great new tip!  vbProperCase....glad you resolved it!
    LVL 23

    Expert Comment

    Is the text / string in a text file to start with or do you have this string within a text box ? If it is in a text file then obviously you would need something like the FSO to read the text file into a variable or textbox or something like that before you could go through it and search for the words you wanted to replace.

    You could have 2 forms, one with the textbox control that had all the text in it and then the 2nd form would have 2 textbox's one with a label next it to search for any words in that string and the 2nd textbox to replace any of the words it finds with the text you enter in the 2nd textbox ie :

    Find      : { dim } <--- TextBox
    Replace : { Dim } <-- TextBox
    Replace        <---- command button

    Then in the command button you would have the code that looped through the string and if it found any words that matched the first textbox ie dim then it would replace them with Dim and then you just type each of the words you wanted to find and replace into the textbox's.

    I am not sure if this is what you wanted, I mean if you could ellaborate on what exactly you want it to do or how you want it to work, then obviously that would help us with regards to giving you a solution that you are after or are happy with.

    If you could please post back with a more detailed description :)
    LVL 4

    Accepted Solution

    Here you go:

    Private Function ReplaceMulti(MainString As String, ParamArray Info() As Variant) As String

        Dim i As Integer
        ReplaceMulti = MainString
        For i = 0 To UBound(Info) Step 2
            ReplaceMulti = Replace(ReplaceMulti, Info(i), Info(i + 1))
    End Function

    Call it like:

    MsgBox ReplaceMulti(Text1.Text, "a", "A", "b", "B", "c", "C")

    which will replace a with A, b wth B, c with C, etc., you can put in an infinate amount.
    LVL 4

    Expert Comment

    Have you found a solution yet?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now