How to concatenate cell columns and keep text color in Excel?
As we all known, while concatenating or combining cell columns into one column, the cell formatting (such as text font color, number formatting, etc) will be lost. This article, I will introduce some tricks to combine the cell columns into one and keep the text color as easily as possible in Excel.
To finish this task in Excel, the following VBA code may do you a favor, please do as this:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: concatenate cell columns and keep text color:
Sub MergeFormatCell() 'Updateby Extendoffice Dim xSRg As Range Dim xDRg As Range Dim xRgEachRow As Range Dim xRgEach As Range Dim xRgVal As String Dim I As Integer Dim xRgLen As Integer Dim xSRgRows As Integer Dim xAddress As String On Error Resume Next xAddress = ActiveWindow.RangeSelection.Address Set xSRg = Application.InputBox("Please select cell columns to concatenate:", "KuTools For Excel", xAddress, , , , , 8) If xSRg Is Nothing Then Exit Sub xSRgRows = xSRg.Rows.Count Set xDRg = Application.InputBox("Please select cells to output the result:", "KuTools For Excel", , , , , , 8) If xDRg Is Nothing Then Exit Sub Set xDRg = xDRg(1) For I = 1 To xSRgRows xRgLen = 1 With xDRg.Offset(I - 1) .Value = vbNullString .ClearFormats Set xRgEachRow = xSRg(1).Offset(I - 1).Resize(1, xSRg.Columns.Count) For Each xRgEach In xRgEachRow .Value = .Value & Trim(xRgEach.Value) & " " Next For Each xRgEach In xRgEachRow xRgVal = xRgEach.Value With .Characters(xRgLen, Len(Trim(xRgVal))).Font .Name = xRgEach.Font.Name .FontStyle = xRgEach.Font.FontStyle .Size = xRgEach.Font.Size .Strikethrough = xRgEach.Font.Strikethrough .Superscript = xRgEach.Font.Superscript .Subscript = xRgEach.Font.Subscript .OutlineFont = xRgEach.Font.OutlineFont .Shadow = xRgEach.Font.Shadow .Underline = xRgEach.Font.Underline .ColorIndex = xRgEach.Font.ColorIndex End With xRgLen = xRgLen + Len(Trim(xRgVal)) + 1 Next End With Next I End Sub
3. Then press F5 key to run this code, and a prompt box is popped out to remind you to select the data range you want to combine with text color, see screenshot:
4. And then click OK, another dialog box is appeared, please select the cells where you want to output the combined results, see screenshot:
5. Then click OK button, the columns have been concatenated without losing the text color as following screenshot shown:
Note: If the data which needed to be combine includes numbers, then macro code cannot work correctly.