Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Remove Middle Initial

Hey Anyone,

I have a range that contails several names such as:

Smith, John D
Dean, James L & Mouse, Mickey M
Trump, Donald
Wayne, John P
Wayne, Bruce

I am trying to remove all the middle initials, which as you can see,
some cells contain a name with no middle initials, and some cell
contain 2 names each with an initial. I'm wondering what the code
would look like to search each cell in the range for a single
character (ie. the middle initial), ignoring "&", and remove the
initial.

Any help would be much appreciated.

Thank You,
Kyle
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Remove Middle Initial

Hi
Try this. I've used the worksheetfunction TRIM as it does more
trimming than the VBA one!
The code first removes excess spaces and replaces " , " with ", " so
that a comma doesn't get removed in the loop. The loop looks for " * "
where * is a single character and replaces it with a space. This
reduces the length of the string so that must be reset in the loop.
The final if deals with a " *" at the end of the string.

Function StripInitial(NameString As String) As String
Dim StringLength As Long, i As Long
Dim TempString As String

StringLength = Len(NameString)
'remove any double spaces
TempString = Application.WorksheetFunction.Trim(NameString)
'don't treat a comma like a single initial
TempString = Replace(TempString, " , ", ", ")
For i = 1 To StringLength - 2
If Mid(TempString, i, 1) = " " Then
If Mid(TempString, i + 2, 1) = " " Then
TempString = Replace(TempString, Mid(TempString, i, 3), "
")
End If
End If
StringLength = Len(TempString)
Next i
If Mid(TempString, StringLength - 1, 1) = " " Then
TempString = Left(TempString, Len(TempString) - 2)
End If
StripInitial = TempString
End Function

Might need a more thorough test than I gave it.
regards
Paul

On Nov 25, 7:46*am, wrote:
Hey Anyone,

I have a range that contails several names such as:

Smith, John D
Dean, James L & Mouse, Mickey M
Trump, Donald
Wayne, John P
Wayne, Bruce

I am trying to remove all the middle initials, which as you can see,
some cells contain a name with no middle initials, and some cell
contain 2 names each with an initial. I'm wondering what the code
would look like to search each cell in the range for a single
character (ie. the middle initial), ignoring "&", and remove the
initial.

Any help would be much appreciated.

Thank You,
Kyle


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Remove Middle Initial

Hi
This is better! Forgot about leaving the & alone. Also, not a good
idea to change loop parameter in the loop..

Function StripInitial(NameString As String) As String
Dim StringLength As Long, i As Long
Dim TempString As String
StringLength = Len(NameString)
'remove any double spaces
TempString = Application.WorksheetFunction.Trim(NameString)
'don't treat a comma like a single initial
TempString = Replace(TempString, " , ", ", ")
For i = StringLength - 2 To 1 Step -1
If Mid(TempString, i, 1) = " " Then
If Mid(TempString, i + 2, 1) = " " Then
If Mid(TempString, i + 1, 1) < "&" Then
TempString = Replace(TempString, Mid(TempString, i,
3), " ")
End If
End If
End If
Next i
StringLength = Len(TempString)
If Mid(TempString, StringLength - 1, 1) = " " Then
TempString = Left(TempString, Len(TempString) - 2)
End If
StripInitial = TempString
End Function

You can use it like

For each Cell in MyRange
Cell.Value = StripInitial(Cell.Value)
next Cell

or use it as a worksheetfunction in the normal way
regards
Paul
Paul
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Remove Middle Initial

On Tue, 25 Nov 2008 05:07:38 -0500, Ron Rosenfeld
wrote:

On Mon, 24 Nov 2008 23:46:07 -0800 (PST), wrote:

Hey Anyone,

I have a range that contails several names such as:

Smith, John D
Dean, James L & Mouse, Mickey M
Trump, Donald
Wayne, John P
Wayne, Bruce

I am trying to remove all the middle initials, which as you can see,
some cells contain a name with no middle initials, and some cell
contain 2 names each with an initial. I'm wondering what the code
would look like to search each cell in the range for a single
character (ie. the middle initial), ignoring "&", and remove the
initial.

Any help would be much appreciated.

Thank You,
Kyle


You can do that with Regular Expressions.

This code will remove any single character that is preceded by another word
consisting of one or more characters. The single character may optionally be
followed by a dot <..

To be more precise, the code returns everything except the MI (and optional
".").

==========================================
Option Explicit
Function NoMI(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.IgnoreCase = True
re.Global = True
re.Pattern = "([A-Z]+)\s\b[A-Z]\b\.?"
NoMI = re.Replace(str, "$1")
End Function
=========================================



--ron


One slight change to handle a name like:

James, L. J.

to return James, L.

--------------------------
'Option Explicit
Function NoMI(str As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.IgnoreCase = True
re.Global = True
re.Pattern = "([A-Z]+)\.?\s\b[A-Z]\b\.?"
NoMI = re.Replace(str, "$1")
End Function
------------------------------------
--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Remove Middle Initial

Maybe this is simpler:

Function remmiddle(wholestr)
Dim joinarr()
splitarr = Split(wholestr, " ")
meret = UBound(splitarr)
j = 0
For a = 0 To meret
If Len(splitarr(a)) 1 Then
ReDim Preserve joinarr(j)
joinarr(j) = splitarr(a)
j = j + 1
End If
Next a
remmiddle = Join(joinarr)
End Function

Regards,
Stefi


€¯ ezt Ć*rta:

Hey Anyone,

I have a range that contails several names such as:

Smith, John D
Dean, James L & Mouse, Mickey M
Trump, Donald
Wayne, John P
Wayne, Bruce

I am trying to remove all the middle initials, which as you can see,
some cells contain a name with no middle initials, and some cell
contain 2 names each with an initial. I'm wondering what the code
would look like to search each cell in the range for a single
character (ie. the middle initial), ignoring "&", and remove the
initial.

Any help would be much appreciated.

Thank You,
Kyle

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Remove Middle Initial

On Tue, 25 Nov 2008 02:13:00 -0800, Stefi
wrote:

Maybe this is simpler:

Function remmiddle(wholestr)
Dim joinarr()
splitarr = Split(wholestr, " ")
meret = UBound(splitarr)
j = 0
For a = 0 To meret
If Len(splitarr(a)) 1 Then
ReDim Preserve joinarr(j)
joinarr(j) = splitarr(a)
j = j + 1
End If
Next a
remmiddle = Join(joinarr)
End Function

Regards,
Stefi


Your UDF will remove the First initial for a name like

Franklin, L Justin

and if the initial is followed by "." , it will not remove them at all.
--ron
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Remove Middle Initial

Thank all of you for your responses. The one that worked best for me
was Paul's. I will not have names such as James, L. J. in my range,
but I do appreciate everyones posts.

Thank You,
Kyle
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Remove Middle Initial

Hi Ron,

I don't understand the first part of your post: my UDF will remove not only
the first but all initials if they are one character long.

No doubt, it will not remove initials followed by "." but none of the
initials in the OP example is followed by ".".

Regards,
Stefi


€˛Ron Rosenfeld€¯ ezt Ć*rta:

On Tue, 25 Nov 2008 02:13:00 -0800, Stefi
wrote:

Maybe this is simpler:

Function remmiddle(wholestr)
Dim joinarr()
splitarr = Split(wholestr, " ")
meret = UBound(splitarr)
j = 0
For a = 0 To meret
If Len(splitarr(a)) 1 Then
ReDim Preserve joinarr(j)
joinarr(j) = splitarr(a)
j = j + 1
End If
Next a
remmiddle = Join(joinarr)
End Function

Regards,
Stefi


Your UDF will remove the First initial for a name like

Franklin, L Justin

and if the initial is followed by "." , it will not remove them at all.
--ron



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Remove Middle Initial

On Wed, 26 Nov 2008 03:48:01 -0800, Stefi
wrote:

Hi Ron,

I don't understand the first part of your post: my UDF will remove not only
the first but all initials if they are one character long.

No doubt, it will not remove initials followed by "." but none of the
initials in the OP example is followed by ".".

Regards,
Stefi


Yours will remove Initials that are not "Middle Initials"

e.g.

Smith, L James

In this case, L is a "first name" initial, not a middle initial, but your
routine returns:

Smith, James
--ron
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default Remove Middle Initial

I see now, I had no idea that one has to take into account that English names
can occur in "Smith, L James" like format that is an initial stands for the
first name followed by an entire name as middle name.

Stefi

€˛Ron Rosenfeld€¯ ezt Ć*rta:

On Wed, 26 Nov 2008 03:48:01 -0800, Stefi
wrote:

Hi Ron,

I don't understand the first part of your post: my UDF will remove not only
the first but all initials if they are one character long.

No doubt, it will not remove initials followed by "." but none of the
initials in the OP example is followed by ".".

Regards,
Stefi


Yours will remove Initials that are not "Middle Initials"

e.g.

Smith, L James

In this case, L is a "first name" initial, not a middle initial, but your
routine returns:

Smith, James
--ron

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract Middle Initial from Name Dave Y Excel Worksheet Functions 10 July 11th 07 11:17 PM
Extract 1st name & middle initial mikeburg[_76_] Excel Programming 5 February 7th 06 10:47 PM
Remove middle initial from "first name middle initial" Justin F. Excel Discussion (Misc queries) 15 September 26th 05 06:13 PM
First name, Middle Initial wnfisba Excel Discussion (Misc queries) 2 September 21st 05 03:03 PM
Stripping Middle Initial from a Name mira Excel Worksheet Functions 13 August 6th 05 08:46 PM


All times are GMT +1. The time now is 02:53 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"