Skip to main content

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.

Concatenate cell columns and keep text font color with VBA code


Concatenate cell columns and keep text font color with VBA code

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
			.NumberFormat = "@"
            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:

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (14)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi Team,

Thanks a lot for this code! Noticed that it would not take in the text colour if the colour is obtained through conditional formatting. Is there a workaround for it?
This comment was minimized by the moderator on the site
Hello, hack,
I'm sorry, at present, there is no better way to preserve conditional formatting colors when merging cells.

If anyone else has a solution, please share it here as a comment.

Thank you!
This comment was minimized by the moderator on the site
5. Note: If the data which needed to be combine includes numbers, then macro code cannot work correctly.

However, if you insert code:
.NumberFormat = "@"
(after:
.ClearFormats
) betwen 24-25 line numbers, maybe work.
This comment was minimized by the moderator on the site
Hello, Mariusz
Thanks a lot for your comment, it works well. We have updated the code in this article.
Thanks again! 👍
This comment was minimized by the moderator on the site
You're welcome! I like 🥰 the very different and interesting tips on this site.
This comment was minimized by the moderator on the site
thanks for the code!
Can it be automatically update the concatenate column if any change make?
This comment was minimized by the moderator on the site
Hi, Kathy,
Sorry, at present, there is no good way for doing that, you need to run the code again after changing the cell content. 😂
Thank you!
This comment was minimized by the moderator on the site
Alright, thanks for your reply
This comment was minimized by the moderator on the site
Thanks for the code.
If the text has "( )",  for example "tomate (tomates)". It doesn't work as well. Is there something to modify in order to work with "( ) "?
This comment was minimized by the moderator on the site
Hello Rah,
How are you? If the text has "( )", we don't need to change the VBA code. Please see the screenshot, the text "tomato (tomatoes)" can be successfully combined with other cell columns into one column. Hope it may help you.
Sincerely,
Mandy
This comment was minimized by the moderator on the site
Habría manera de hacerlo con números?
This comment was minimized by the moderator on the site
Fantástica la macro de concatenar cadena de texto conservando formato de origen. En mi caso no funcionaba con F5, pero en la pestaña macro, modificar y opciones me permite entrar el modo de ejecucion con CTRL+letra y funciona de maravilla. Muchas gracias.
This comment was minimized by the moderator on the site
Hello,
This methods works well, until xRgLen is higher than 255.
It looks this parameter is a byte :-(
How can this limitation been overtaken ?
This comment was minimized by the moderator on the site
This code works really well. But, how do I add space before or after the text?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations