How to Split "CamelCase" Text in cells

Seth McGraw

New Member
Joined
Feb 6, 2009
Messages
1
I have several incorrectly formatted files that contain camel case in cells that should be split.
example: LastnameFirstname should be Lastname "tab" Firstname

There is no option for text to columns because there is no delimiter to use for the split.
Does anyone know how to (via formula hopefully, but macro or vb is ok if I can learn how to use it):

----put a space (or other delimiter) before the second and any concurent Capital letters so that the text to columns can be used
or
----split the names in one swoop

Thanks in advance,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Select your data then run this code:
Code:
Sub SplitData()
    Dim RegExp As Object, match
    Dim strPattern As String, strText As String
    Dim rngCell As Range, lngOffset As Long
    Set RegExp = CreateObject("vbscript.regexp")
    strPattern = "[A-Z][^A-Z]*"
    With RegExp
        .Global = True
        .Pattern = strPattern
        For Each rngCell In Selection
            Set match = .Execute(rngCell.Value)
            If match.Count Then
               If match(0).FirstIndex > 1 Then
               rngCell.Value = left$(rngCell.Value, match(0).FirstIndex - 1)
               For lngOffset = 1 To match.Count
                rngCell.Offset(0, lngOffset) = match(lngOffset - 1)
                Next lngOffset
               Else
               For lngOffset = 0 To match.Count - 1
                rngCell.Offset(0, lngOffset) = match(lngOffset)
                Next lngOffset
               End If
            End If
        Next rngCell
    End With
End Sub
 
Upvote 0
Hi

Another option:

Code:
Sub CamelCase()
Dim rCell As Range
Dim lCount As Long

With CreateObject("vbscript.regexp")
    .Pattern = "([a-z])([A-Z])"
    .Global = True
    For Each rCell In Selection
        lCount = .Execute(rCell).Count
        If lCount Then rCell.Resize(, lCount + 1) = Split(.Replace(rCell, "$1" & Chr(1) & "$2"), Chr(1))
    Next rCell
End With
End Sub


Before and after:

00 HTML Conversions.xlsm
A
1
2ClarkKent
3LoisLane
4LanaLang
5LexLuthor
6FirstSecondThirdNames
CamelCase


00 HTML Conversions.xlsm
ABCD
1
2ClarkKent
3LoisLane
4LanaLang
5LexLuthor
6FirstSecondThirdNames
CamelCase
 
Last edited by a moderator:
Upvote 0
Here's how to split camelCase text using only a formula, no VBA required. For this formula, the camelCase text is in cell A1.

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"A"," a"),"B"," b"),"C"," c"),"D"," d"),"E"," e"),"F"," f"),"G"," g"),"H"," h"),"I"," i"),"J"," j"),"K"," k"),"L"," l"),"M"," m"),"N"," n"),"O"," o"),"P"," p"),"Q"," q"),"R"," r"),"S"," s"),"T"," t"),"U"," u"),"V"," v"),"W"," w"),"X"," x"),"Y"," y"),"Z"," z")
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

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