by Allen Wyatt
(last updated January 5, 2019)
Feroz has a series of text values in column A. These values are formatted with two different font colors in each cell. (First part of text is one color and the second part is a different color.) He would like to split these text values into columns B and C, such that anything with the first color is in column B and anything with the second color is in column C. He notes that the Text to Columns tool won't handle this, so he wonders if it can be done.
Yes, it can be done. One way is to use Excel's built-in Flash Fill capability. (This tool is available only in Excel 2013 or later versions.) Let's say you are starting with data that looks like this: (See Figure 1.)
Figure 1. Your multi-colored data.
Note that my testing data includes, in column E, some characteristics of the data in column A. At this point, all you need to do is to give Flash Fill something it can work with. I do this by manually breaking apart the text in rows 2 and 3, as shown here: (See Figure 2.)
Figure 2. Setting up the examples.
It is important that the examples you create in B2:C3 are exact—they should include anything that is in whatever color (including leading or trailing spaces) and both spelling and capitalization should be correct.
Now select cell B4 and press Ctrl+E. This makes Flash Fill spring into action, and you will see in text appear in the rest of column B. Do the same thing in column C—select cell C4 and press Ctrl+E. Your results should look similar to the following: (See Figure 3.)
Figure 3. After using Flash Fill in both columns.
I should note that your success with Flash Fill will depend, in large part, on the characteristics of the data in column A. It will, in most cases, do the majority of the work, and it may complete the task tremendously. There may be some strange instances in which Flash Fill cannot discern how it should pull apart your data. You can see this in the previous figure where cells A7, A9, and A11 were not pulled apart correctly. You'll want to check your results carefully to make sure they make sense.
If Flash Fill doesn't work for you, then you will want to create a macro to do the work. It is possible to come up with a macro that will work on all the cells in column A and pull the text into columns B and C (like Feroz needs), but it is more flexible to create a user-defined function that will return whatever is wanted from the cell. Here's an example:
Function SplitColors(r As Range, Optional iWanted As Integer = 1) _ As String Dim sTemp As String Dim J As Integer Dim K As Integer Dim iColors(9) As Integer sTemp = "" If r.Cells.Count = 1 Then For J = 1 To 9 iColors(J) = 0 Next J ' Determine where colors change ' Remember there will always be at least one color K = 1 iColors(K) = 1 For J = 2 To Len(r.Text) If r.Characters(J,1).Font.Color <> _ r.Characters(J-1,1).Font.Color Then K = K + 1 iColors(K) = J End If Next J ' Check if wanted color is less than total colors If iWanted <= K Then J = iColors(iWanted + 1) If J = 0 Then J = Len(r.Text) + 1 J = J - iColors(iWanted) sTemp = Mid(r.Text, iColors(iWanted), J) End If End If SplitColors = sTemp End Function
The SplitColors function requires one parameter (a range to act upon) and a second, optional parameter (which color from the range you want). The function checks, first, to see if it was passed a single cell. If so, then it determines how many colors are in that cell and the character numbers where the color changes occur. Then, if the desired color (passed in the optional second parameter) is less than the number of colors in the cell, the characters using that color are returned.
So, for instance, if you want to return the cells using the first color in cell A2, you could use either of the following in your worksheet:
=SplitColors(A2, 1) =SplitColors(A2)
The second invocation works because the second parameter is optional. If you do not include it, then the function assumes you want to work with the first color. If you want to return the text using the second color in the cell, then the following will work:
The SplitColors function will work with up to 9 colors in whatever cell you are checking out. If you specify a second parameter of 9 or greater, then you'll end up with an error.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13605) applies to Microsoft Excel 2013, 2016, 2019, and Excel in Office 365.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
If you have a lot of text in your workbook, at some point you might want to split out sentences into individual cells. ...Discover More
The easy way to get rid of spaces at the beginning or end of a cell's contents is to use the TRIM function. ...Discover More
If you have added subtotals to your worksheet data, you might want to copy those subtotals somewhere else. This is easy ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.