vba help please

shyamvinjamuri

Board Regular
Joined
Aug 2, 2006
Messages
175
split a name into first and last name based on upper and lower case.
eg. JohnDoe to read as John Doe.

Please help
Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I fiddled around with it some, and there is probably a shorter way to do this, but the below options work.
Excel Workbook
ABC
1JohnDoeJohnDoe
2MikeWilfordMikeWilford
3JaniceDoeJaniceDoe
4MichaelWilfordMichaelWilford
5JohnDoeJohnDoe
6MikeWilfordMikeWilford
7JaniceDoeJaniceDoe
8MichaelWilfordMichaelWilford
Sheet2
Excel 2007
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
 
Upvote 0
I could not find my previous post
Look at the blue bar at the top of the page and click Quick Links|Subscribed Threads.

Alternatively, again in the blue bar: Search|Advanced Search then you can search by your user name and other parameters to narrow your search if you want.
 
Upvote 0
shyamvinjamuri,


Sample data before the macro:


Excel Workbook
A
1JohnDoe
2MikeWilford
3JaniceDoe
4MichaelWilford
5JohnDoe
6MikeWilford
7JaniceDoe
8MichaelWilford
9
Sheet1





After the macro:


Excel Workbook
A
1John Doe
2Mike Wilford
3Janice Doe
4Michael Wilford
5John Doe
6Mike Wilford
7Janice Doe
8Michael Wilford
9
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub SplitName()
' hiker95, 03/04/2011
' http://www.mrexcel.com/forum/showthread.php?t=533666
Dim c As Range, a As Long, H As String
Application.ScreenUpdating = False
For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
  H = Trim(c)
  For a = 2 To Len(H) Step 1
    If Asc(Mid(H, a, 1)) > 64 And Asc(Mid(H, a, 1)) < 91 Then
      c = Left(H, a - 1) & " " & Right(H, Len(H) - a + 1)
      Exit For
    End If
  Next a
Next c
Application.ScreenUpdating = True
End Sub


File SaveAs your workbook as a Macro Enabled workbook with extension xlsm.


Then run the SplitName macro.
 
Upvote 0
Here's my effort. Paste this into the worksheet's code window and forget about it.

Code:
Option Explicit
 
Private Sub Worksheet_Calculate()
  Call InsertSpaces
End Sub
 
Private Sub Worksheet_Change(ByVal Target As Range)
  Call InsertSpaces
End Sub
 
Private Sub InsertSpaces()
 
  Const sNameColumn As String = "A" [COLOR=green]' column containing name[/COLOR]
 
  Dim iLast As Long
  Dim iRow As Long
  Dim iChar As Integer
  Dim iByte1 As Byte
  Dim iByte2 As Byte
  Dim sTemp As String
  
  Application.EnableEvents = False
  iLast = Cells(Rows.Count, sNameColumn).End(xlUp).Row
  For iRow = 1 To iLast
    Cells(iRow, sNameColumn) = Replace(Cells(iRow, sNameColumn), " ", "")
    sTemp = Left(Cells(iRow, sNameColumn), 1)
    For iChar = 2 To Len(Cells(iRow, sNameColumn))
      iByte1 = Asc(Mid(Cells(iRow, sNameColumn), iChar - 1, 1))
      iByte2 = Asc(Mid(Cells(iRow, sNameColumn), iChar, 1))
      If iByte1 >= Asc("a") And iByte1 <= Asc("z") And iByte2 >= Asc("A") And iByte2 <= Asc("Z") Then
        sTemp = sTemp & " " & Chr(iByte2)
      Else
        sTemp = sTemp & Chr(iByte2)
      End If
    Next iChar
    Cells(iRow, sNameColumn) = sTemp
  Next iRow
  Application.EnableEvents = True
   
End Sub
 
Upvote 0
My suggestions. I've tried to include handling of multiple names, multiple initials, hyphenated names, names that already contain spaces.

Post back if you need help how to implement them.

A) If the split names are to appear in a single cell. Try this User-Defined Function.


<font face=Courier New><br><SPAN style="color:#00007F">Function</SPAN> AddSpaces(s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Static</SPAN> RegEx <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><br>    <SPAN style="color:#00007F">If</SPAN> RegEx <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> RegEx = CreateObject("VBScript.RegExp")<br>        <SPAN style="color:#00007F">With</SPAN> RegEx<br>            .Global = <SPAN style="color:#00007F">True</SPAN><br>            .Pattern = "([a-zA-Z])(?=[A-Z])"<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    AddSpaces = RegEx.Replace(s, "$1 ")<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br></FONT>


Used in the sheet like this (copied down).

Excel Workbook
AB
1JohnDoeJohn Doe
2MikeWilfordMike Wilford
3JaniceDoeJanice Doe
4MichaelWilfordMichael Wilford
5JohnDoeJohn Doe
6ThomasMichaelSmithThomas Michael Smith
7PPJonesP P Jones
8PaulKDWalkerPaul K D Walker
9MichaelJFoxMichael J Fox
10JohnSmith-JonesJohn Smith-Jones
11John SmithJohn Smith
Split Names 1 Cell




B) Each Name/Initial in a separate cell.

Run this macro from a standard module.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> SplitNames()<br>    <SPAN style="color:#00007F">Dim</SPAN> RX <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, ary <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> myRng <SPAN style="color:#00007F">As</SPAN> Range, cl <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> RX = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">Set</SPAN> myRng = Range("A1", Range("A" & Rows.Count).End(xlUp))<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> RX<br>        .Pattern = ".*?[a-zA-Z](?=[A-Z]| |$)"<br>        .Global = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cl <SPAN style="color:#00007F">In</SPAN> myRng<br>            s = cl.Value<br>            <SPAN style="color:#00007F">If</SPAN> .Test(s) <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> ary = .Execute(s)<br>                <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> ary.Count<br>                    cl.Offset(, i).Value = Trim(ary(i - 1))<br>                <SPAN style="color:#00007F">Next</SPAN> i<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> cl<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


Results are:

Excel Workbook
ABCD
1JohnDoeJohnDoe
2MikeWilfordMikeWilford
3JaniceDoeJaniceDoe
4MichaelWilfordMichaelWilford
5JohnDoeJohnDoe
6MikeWilfordMikeWilford
7JaniceDoeJaniceDoe
8MichaelWilfordMichaelWilford
9ThomasMichaelSmithThomasMichaelSmith
10PPJonesPPJones
11MichaelJFoxMichaelJFox
12JohnSmith-JonesJohnSmith-Jones
13John SmithJohnSmith
Split Names Multiple Cells
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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