Vba to split up words in a cell by the capital letters

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,
We down load data and it comes out badly formatted sometimes so we get thing like "NatureFoods Ltd." instead of Nature Foods Ltd

I'd like a macro that can help me separate words that have been stuck together.

what id like is for a macro to go down each cell in range C10:C50

And look at the words in these cells one at a time,
If there is a capital letter amongst lowercase letters add a space, but only if the capital has a lower case in front and behind it

please help if you can

thanks

Tony
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
One way,

SplitByCase.xlsm
CD
9OriginalSplit
10NatureFoods Ltd.Nature Foods Ltd.
11NetWareNet Ware
12LibreOfficeLibre Office
13CyberMaxCyber Max
14SuperDiskSuper Disk
15BlackBerryBlack Berry
16SharePointShare Point
17ExpressCardExpress Card
18VisiCalcVisi Calc
19ImageShackImage Shack
20MacBookMac Book
21FiveThirtyEightFive Thirty Eight
Sheet1


VBA Code:
Sub LUS()
Dim r As Range:         Set r = Range("C10:C" & Range("C" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim b() As Byte

For i = 1 To UBound(AR)
    AR(i, 1) = cSplit(AR(i, 1))
Next i

r.Offset(, 1).Value = AR
End Sub

Function cSplit(s As Variant)
Dim b() As Byte: b = s

For i = UBound(b) - 3 To 2 Step -2
    If b(i - 2) > 96 And b(i - 2) < 123 And b(i) > 64 And b(i) < 91 Then
        s = Application.WorksheetFunction.Replace(s, (i / 2) + 1, 0, " ")
    End If
Next i

cSplit = s
End Function
 
Upvote 0
Or, using Power Query.

SplitByCase.xlsm
AB
1OriginalSplit
2NatureFoods Ltd.Nature Foods Ltd.
3NetWareNet Ware
4LibreOfficeLibre Office
5CyberMaxCyber Max
6SuperDiskSuper Disk
7BlackBerryBlack Berry
8SharePointShare Point
9ExpressCardExpress Card
10VisiCalcVisi Calc
11ImageShackImage Shack
12MacBookMac Book
13FiveThirtyEightFive Thirty Eight
Sheet2


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Split = Table.SplitColumn(Source, "Original", Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}), {"Original.1", "Original.2", "Original.3"}),
    Merge = Table.CombineColumns(Split,{"Original.1", "Original.2", "Original.3"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Split")
in
    Merge
 
Upvote 0
Or dynamic arrays (it's messy)
MrExcelPlayground4.xlsx
AB
30BigBadWolfBig Bad Wolf
31SillyGooseSilly Goose
32Silly GooseSilly Goose
33SillyGOOSESillyGOOSE
34BigBadWolfBig Bad Wolf
Sheet33
Cell Formulas
RangeFormula
B30:B34B30=TEXTJOIN(,TRUE,IF((IF((IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))>64),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))<98),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))>96),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))<123),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))>96)*(CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))<123),0))=0,SEQUENCE(ROWS((IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))>64),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))<98),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))>96),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))<123),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))>96)*(CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))<123),0)))),""))<>"",MID(A30,(IF((IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))>64),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))<98),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))>96),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))<123),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))>96)*(CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))<123),0))=0,SEQUENCE(ROWS((IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))>64),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))<98),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))>96),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))<123),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))>96)*(CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))<123),0)))),"")),1),MID(A30,(IF((IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))>64),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))<98),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))>96),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))<123),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))>96)*(CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))<123),0))=1,SEQUENCE(ROWS((IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))>64),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,2,1),1))<98),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))>96),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,1,1),1))<123),0)*IFERROR((CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))>96)*(CODE(MID(A30,SEQUENCE(LEN(A30),1,3,1),1))<123),0)))),"")),1)&" "))
 
Upvote 0
OMG, lots of options,
thank you all,
i'm going to try lrobbo314 idea first as that looks most suited to my needs
Thank you all very much
Thanks
Tony
 
Upvote 0
Right on.

Here's another VBA option that uses a different approach.

VBA Code:
Sub LUX()
Dim r As Range:         Set r = Range("C10:C" & Range("C" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2

With CreateObject("VBScript.RegExp")
    .Pattern = "([a-z])([A-Z])"
    For i = 1 To UBound(AR)
        AR(i, 1) = .Replace(AR(i, 1), "$1 $2")
    Next i
    r.Offset(, 1).Value = AR
End With
End Sub
 
Upvote 0
One more:

VBA Code:
Function jec(cell) As String
 With CreateObject("VBscript.Regexp")
  .Global = True
  .Pattern = "(?=[A-ZÄÖÜ])"
  jec = Trim(.Replace(Replace(cell, " ", ""), " "))
 End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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