Replace if the Nth Character in a text string is "I" then replace with "Y"

Mohamedazees

New Member
Joined
Oct 18, 2020
Messages
43
Office Version
  1. 2019
Platform
  1. Windows
Dear Sir,

I Want a excel VBA code for replace if the Nth Character of Text strings is "I" the Please the character With "Y" in a Range of Cells

for Example i herewith attached the Sample data image for your ready reference.

Thanks in advance
 

Attachments

  • Example Data.jpg
    Example Data.jpg
    23.1 KB · Views: 22

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Code:
Sub Maybe()
Dim a As Long, b As String, bb As String, c As Range
a = Application.InputBox(Prompt:="Position (nth character) of character.", Title:="Enter a number.", Type:=1)
b = Application.InputBox(Prompt:="Enter the alphabetical letter to be replaced.", Title:="Letter.", Type:=2)
bb = Application.InputBox(Prompt:="Enter the alphabetical letter that replaces it.", Title:="Letter.", Type:=2)
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If Mid(c, a, 1) = b Then c.Value = Left(c, a - 1) & bb & Mid(c, a + 1, 99)
    Next c
End Sub
 
Upvote 0
Rich (BB code):
For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
  If Mid(c, a, 1) = b Then c.Value = Left(c, a - 1) & bb & Mid(c, a + 1, 99)
Next c
Inside the If..Then statement, you use Mid(c, a + 1, 99)... you can remove the third argument. Unlike in Excel formulas, the third argument to the Mid function is optional and, when omitted, returns all of the remaining text after the specified starting point.
 
Upvote 0
Yes, you're right Rick. Now that you mention it, I think you taught me that previously and I had totally forgotten that.
Thanks for reminding me Rick. Appreciated.
 
Upvote 0
Thanks for letting us know that it does what you want it to do.
Good luck
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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