concatenate and keep formatting

kruiser

New Member
Joined
Apr 9, 2007
Messages
48
I have four cells each having text in different color. I want to concatenate them into 1 cell and retain the original formatting. i.e. the result cell should have a + b + c + d with original colors of a, b, c & d.

Is it possible?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Possible, yes - but only with VBA (you can't do it with a native formula, for example). Are you prepared to go a VBA route?
 
Upvote 0
Yes, I am a beginner in VBA, but I should be able to follow if you can give me some pointers.

Thanks!
 
Upvote 0
Not wanting to reinvent the wheel Erik, your code is pretty much how I would approach the problem (once I'd got my head round the nested 'Withs' that is ;-)). It many ways, it's a shame you can't use something like the TEXT function to apply color formats to concatenated values...
 
Upvote 0
I revised it. There is "trim" in one line: should be in all lines, else the format will be wrong. Try out with several leading spaces.
(you can code without "trim" of course)

Code:
Option Explicit

Sub test()
Call concatenate_cells_formats(Range("A1"), Range("C1,C2,C3"))
End Sub

Sub concatenate_cells_formats(cell As Range, source As Range)
'Erik Van Geit
'070607

Dim c As Range
Dim i As Integer

i = 1

    With cell
    .Value = vbNullString
    .ClearFormats
    
        For Each c In source
        .Value = .Value & " " & Trim(c)
        Next c

    .Value = Trim(.Value)

        For Each c In source
            With .Characters(Start:=i, Length:=Len(Trim(c))).Font
            .Name = c.Font.Name
            .FontStyle = c.Font.FontStyle
            .Size = c.Font.Size
            .Strikethrough = c.Font.Strikethrough
            .Superscript = c.Font.Superscript
            .Subscript = c.Font.Subscript
            .OutlineFont = c.Font.OutlineFont
            .Shadow = c.Font.Shadow
            .Underline = c.Font.Underline
            .ColorIndex = c.Font.ColorIndex
            End With
            .Characters(Start:=i + Len(c), Length:=1).Font.Size = 1
        i = i + Len(Trim(c)) + 1
        Next c

    End With

End Sub
best regards,
Erik
 
Upvote 0
I have managed to get this code working for my setup but I have 2 questions:

1. Which part of the code needs to be removed to stop the size 1 spaces being inserted between the data? (Just worked this out :roll: )

2. How do I set it up to use relative cells when I run the macro (ie use the selected cell to enter the concatonated data in)?

Also your new version of the code was extracting the space from the end of one of my cells. The old code does not trim this off

Many thanks

Meblin
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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