CamelCase to Proper Case

scorpio_libra

Board Regular
Joined
Mar 30, 2009
Messages
52
I Am having a data of around 1000 rows. The data is in a camel case for e.g. SanFrancisco, UnitedArabEmirates...etc....i want to convert it into Title case and wherever the formula finds the Capital alphabet prior to that it should leave a single space......in simple terms.....
SanFrancisco should be changed to San Francisco
UnitedArabEmirates should be changed to United Arab Emirates

Already am having a macro for this, however am curious to know that it can be done by Excel Formula if any or not....
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Well I came to this but it doesn't work with UAE for example...SF works fine:
=LEFT(A1,LOOKUP(2^15,FIND($D$1:$D$26,A1,2))-1)&" "&RIGHT(A1,LEN(A1)-LOOKUP(2^15,FIND($D$1:$D$26,A1,2))+1)

A1 holds the string, D1:D27 hold A-Z in capital.
Maybe anyone can finish this for me, I'm stuck.
 
Upvote 0
scorpio_libra,

Excel Workbook
AB
1SanFranciscoSan Francisco
2UnitedArabEmiratesUnited Arab Emirates
Sheet1



The Function/formula in cell B1 (copied down):
=AddSpace(A1)



Press and hold down the 'ALT' key, and press the 'F11' key.

On the 'Insert' menu, click 'Module'.

Copy the below code, and paste it into the Module (on the right pane).


Code:
Option Explicit
Function AddSpace(txt As String) As String
'
' If cell A1 contains: SanFrancisco
' cell B1: =AddSpace(A1)
' cell B1: San Francisco
'
Dim Hold As String, i As Long
Hold = Left(txt, 1)
For i = 2 To Len(txt) Step 1
  If Asc(Mid(txt, i, 1)) > 96 Then
    Hold = Hold & Mid(txt, i, 1)
  Else
    Hold = Hold & " " & Mid(txt, i, 1)
  End If
Next i
AddSpace = Hold
End Function


Have a great day,
Stan
 
Upvote 0
With a UDF:

Code:
Function FixSpacing(r As String) As String
With CreateObject("vbscript.regexp")
    .Pattern = "([a-z])([A-Z])"
    .Global = True
    FixSpacing = .Replace(r, "$1 $2")
End With
End Function

Excel Workbook
AB
1UnitedArabEmiratesUnited Arab Emirates
2SanFranciscoSan Francisco
Sheet1
 
Upvote 0

Forum statistics

Threads
1,224,656
Messages
6,180,195
Members
452,968
Latest member
TaomanhducPro90

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