Multiple Different Finds and replacing the same in Textbox with Single String or character at one go

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
688
Hello
Below is the code to execute multiple different finds and replacing the same in Textbox with Single String or Character at one go but some how could not succeed
VBA Code:
Sub MultiReplacements()
Dim arrayAsciichar() As Variant, i As Long

arrayAsciichar = Array("-", "_", ":", " ", "#", "$", "*")

For i = LBound(arrayAsciichar) To UBound(arrayAsciichar)
If InStr(txtSearchFor.Text, arrayAsciichar(i)) > 0 Then
     txtReplace.Text = Replace(txtSearchFor.Text, arrayAsciichar(i), cmbInsertChars.Text)
End If

Next i
End Sub
for eg if txtSearchFor.Text = "MrExcel-Thread*1111"
so after executing the above code i get below result as after incorporating the value "#" in combobox
txtReplace.Text = "MrExcel-Thread#1111" instead of "MrExcel#Thread#1111"

Any possibilities for multiple finds and replacing same with Single String or character at One go in a textbox using the above loop

NimishK
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You are always replacing the 'original' txtSearchFor.Text.

Place the original into txtReplace first, then keep replacing on the txtReplace.
VBA Code:
txtReplace.Text = txtSearchFor.Text
For i = LBound(arrayAsciichar) To UBound(arrayAsciichar)
If InStr(txtSearchFor.Text, arrayAsciichar(i)) > 0 Then
     txtReplace.Text = Replace(txtReplace.Text, arrayAsciichar(i), cmbInsertChars.Text)
End If
 
Upvote 0
Solution
aRandomHelper
You are always replacing the 'original' txtSearchFor.Text.

Place the original into txtReplace first, then keep replacing on the txtReplace.
Thank you for your interchangibility option. I thought solution was only from one option which i coded and did not seriously look from your point and other ways.

Thanks a tonne. ? Works Perfect ??

NimishK
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top