Hoppa till huvudinnehåll

Hur sammanfoga cellkolumner och behålla textfärg i Excel?

När vi sammanfogar eller kombinerar cellkolumner i en kolumn kommer cellformateringen att gå förlorad (som vi alla känner till). Den här artikeln kommer jag att presentera några knep för att kombinera cellkolumnerna i en och hålla textfärgen så lätt som möjligt i Excel.

Sammanfoga cellkolumner och behåll texttypsnittsfärg med VBA-kod


Sammanfoga cellkolumner och behåll texttypsnittsfärg med VBA-kod

För att slutföra denna uppgift i Excel kan följande VBA-kod göra dig en tjänst, vänligen gör så här:

1. Håll ner ALT + F11 nycklar för att öppna Microsoft Visual Basic för applikationer fönster.

2. Klicka Insert > Modulernaoch klistra in följande kod i Modulfönster.

VBA-kod: sammanfoga cellkolumner och behåll textfärg:

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. Tryck sedan på F5 för att köra den här koden, och en uppmaningsruta dyker upp för att påminna dig om att välja det dataintervall du vill kombinera med textfärg, se skärmdump:

4. Och klicka sedan på OK, en annan dialogruta visas, välj cellerna där du vill mata ut de kombinerade resultaten, se skärmdump:

5. Klicka sedan OK knappen har kolumnerna sammanfogats utan att textfärgen förloras som följande skärmdump visas:

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
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