+ Reply to Thread
Results 1 to 5 of 5

Separating Cells by Capital Letter.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    Brighton
    MS-Off Ver
    Excel 2007
    Posts
    3

    Separating Cells by Capital Letter.

    Hi All.

    I would be very grateful if someone could help with the following:

    I am looking for a formula that separates cells by capital letter:

    so If I have "BlueShoes", I would end up with cells containing "Blue" and "Shoes".

    I need this for varying lengths of words, so for example "ReallyBloodyNiceBlueShoes" would be split to "Really", "Bloody", "Nice" and "Shoes".

    I have found the attached file online, however, I can't work out how to adjust it for words with more that two words.

    Positive feedback will be given for all help.

    Thanks in advance.

    Ryan.

    16391_Splitting data in cells with capital letters.xls

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Separating Cells by Capital Letter.

    Try running this macro when the cells are selected.

    Sub SplitByCapitals()
    Dim Cell As Range
    For Each Cell In Selection
        X = 2
        For N = 1 To Len(Cell)
            If Mid(Cell, N, 1) = UCase(Mid(Cell, N, 1)) Then
                X = X + 1
            End If
            Cells(Cell.Row, X) = Cells(Cell.Row, X) & Mid(Cell, N, 1)
        Next N
    Next Cell
    End Sub
    Open up the VBA editor by hitting ALT F11

    Insert a new module by hitting Insert - Module

    Paste the macro into the empty sheet

    Hit ALT F11 to get back to the worksheet.

    Run the macro by going to tools-macro in Excel 2003 or the view ribbon in Excel 2007/2010.

  3. #3
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,421

    Re: Separating Cells by Capital Letter.

    I've extended the formulae out to column H, so that you can have up to 7 words, but I had to save the file as .xlsx because of the level of nesting (but you have XL2007, so that's okay).

    However, if you have 2 or more words with the same initial capital letter it doesn't work properly, as can be seen in the example file.

    Hope this helps.

    Pete

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Separating Cells by Capital Letter.

    I'd be very tempted to do this in VB, via a user-defined function.

    Something like this, perhaps (not very neat, but it works):

    Function SplitOnCapitals(ByVal sWordToSplit As String, lElement As Long) As String
    
    Dim sElements() As String
    Dim lCharLoop As Long
    Dim bFirstMatch As Boolean
    Dim sTmp As String
    
    If sWordToSplit = LCase(sWordToSplit) Then
      SplitOnCapitals = sWordToSplit
    Else
      bFirstMatch = True
      sTmp = Left(sWordToSplit, 1)
      sWordToSplit = sWordToSplit & "A"
      For lCharLoop = 2 To Len(sWordToSplit)
        If Mid(sWordToSplit, lCharLoop, 1) = UCase(Mid(sWordToSplit, lCharLoop, 1)) Then
          If bFirstMatch Then
            ReDim sElements(1 To 1)
            bFirstMatch = False
          Else
            ReDim Preserve sElements(1 To UBound(sElements) + 1)
          End If
          sElements(UBound(sElements)) = sTmp
          sTmp = Mid(sWordToSplit, lCharLoop, 1)
        Else
          sTmp = sTmp & Mid(sWordToSplit, lCharLoop, 1)
        End If
      Next lCharLoop
      
      If lElement > UBound(sElements) Then
        SplitOnCapitals = ""
      Else
        SplitOnCapitals = sElements(lElement)
      End If
      
    End If
    With this in a module you could use:

    =SPLITONCAPITALS("ILikeIceCreamCones",3)

    And it would return "Ice", the 3rd capitalized word in the string.

    If your data was in cell A1 then you could go with:

    =SPLITONCAPITALS($A1,COLUMN(A1))

    And drag across 10 columns (or however many words you expect to have) and have it return each word in sequence.

    Probably easier than trying to do it in formula.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Separating Cells by Capital Letter.

    Hi,

    Not a complete answer yet but I can't help thinking there's an array formula here.

    Formula: copy to clipboard

    =IF(CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))<91,ROW(INDIRECT("1:"&LEN(A1))),"")

    entered as an array formula will return the positions of every capital letter, i.e. 1,7,13,17 & 21.

    Haven't got time just at the moment to play around with this but I suspect this can in some way be combined with a SUBSTITUTE formula to introduce a delimiter symbol to the original string, and then a simple Text To Columns will create columns from the string.

    Feel free to play around with it. When I get a bit more time I'll take another look unless someone's come up with the answer in the meantime.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1