Need VBA code to find specific characters and eliminate

skyport

Active Member
Joined
Aug 3, 2014
Messages
374
What I need to be able to do is have the VBA eliminate any an all letters whenever they are both preceded by a blank space(s) PLUS followed by a period and one or more spaces. However, never delete the letters when any other character is before or after them.

Examples:

( M. )
( P. )

The goal is to eliminate all middle intitials in names and just leave/preserve the first and last names and surrounding data before and after the names

Hoping someone can help.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try something like this.
Change the column to suit
First test it on a copy of your data.

Code:
Columns("A").Replace " ?. ", " ", xlPart
 
Last edited:
Upvote 0
Code:
[color=darkblue]Sub[/color] Macro1()
    Columns("A").Replace " ?.", "", xlPart
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
I did try it on a more recennt excel version and it did run however, it does the following:


Original data before the code

John D. Smith, M.D.
Patricia F. Jones, M.D.


Data after code

John Smith,D.
Patricia Jones,D.

<tbody>
</tbody>

It properly eliminates the middle initial and period but also takes away from the M.D. which is the flaw.
 
Upvote 0
Got it to work from your initial idea. Changed the code as follows and it works.


Your original code -

Sub Macro1()
Columns("A").Replace " ?.", "", xlPart
End Sub


Change I made -

Sub Macro1()
Columns("A").Replace " ? .", " ", xlPart
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,993
Members
449,480
Latest member
yesitisasport

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