+ Reply to Thread
Results 1 to 5 of 5

Combining Contents of two cells whilst preserving text coloring

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Combining Contents of two cells whilst preserving text coloring

    Hi Guru's!

    I've been stuck trying to solve a problem for combining the contents of two cells into one cell.

    I am trying to Maintain the cell formatting - in particular the text color - of the two cells, however the text always seems to revert to the default color of the cell - and all other formats are lost.

    Is there a way around this so that when i combine the values of two cells the colors of the text in both cells is preserved?

    Kind regards
    Jordan
    Last edited by jordan2322; 09-11-2013 at 12:20 AM.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Combining Contents of two cells whilst preserving text coloring

    What method are you using to combine? VBA?

    Maybe this?
    Range("A1").Value = Range("A1").Value & Range("B1").Value
    Range("B1").ClearContents

  3. #3
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Combining Contents of two cells whilst preserving text coloring

    Thanks for your reply Millz, I was thinking of using something like this iterative loop to add and color one letter at a time

    
        iExisting_Length = Len(ActiveSheet.Cells(ActiveCell.Row - i, ActiveCell.Column))
        
        For iChar = 1 To Len(ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column))
            
            Char = Mid(ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column).Value, iChar, 1)
            
            ActiveSheet.Cells(ActiveCell.Row - i, ActiveCell.Column).Value = ActiveSheet.Cells(ActiveCell.Row - i, ActiveCell.Column).Value & Char
            
            With ActiveSheet.Cells(ActiveCell.Row - i, ActiveCell.Column).Characters(Start:=iExisting_Length + iChar, Length:=1).Font
            
                .ColorIndex = ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column).Characters(Start:=iChar, Length:=1).Font.ColorIndex
                
            End With
            
        Next iChar
    That may be a bit confusing so I've simplified it to this (assume that all necessary variable declarations are made)
        iExisting_Length = Len(combined_cell.Value)
        
        For iChar = 1 To Len(other_cell.Value)
            
            Char = Mid(other_cell.Value, iChar, 1)
            
            combined_cell.Value = combined_cell.Value & Char
            
            With combined_cell.Characters(Start:=iExisting_Length + iChar, Length:=1).Font
            
                .ColorIndex = other_cell.Characters(Start:=iChar, Length:=1).Font.ColorIndex
                
            End With
            
        Next iChar
    Last edited by jordan2322; 09-09-2013 at 10:11 PM.

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Combining Contents of two cells whilst preserving text coloring

    Ok, so coloring by character is a little tricky. Here is what I did:
        a = Len(Range("A1"))
        b = Len(Range("B1"))
        Range("Z1").Value = Range("A1").Value & Range("B1").Value
        For k = 1 To a
            Range("Z1").Characters(k, 1).Font.Color = Range("A1").Characters(k, 1).Font.Color
        Next
        For k = 1 To b
            Range("Z1").Characters(k + a, 1).Font.Color = Range("B1").Characters(k, 1).Font.Color
        Next
        Range("Z1").Copy Range("A1")
        Range("Z1").ClearContents
    I combine the 2 cells in a third temporary cell, then color it accordingly based on the 2 original cells, and copy it over to the first cell. Maybe you can try this method?

  5. #5
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Cool Re: Combining Contents of two cells whilst preserving text coloring

    perfect thanks for your help millz!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Waterfall Chart with automatic coloring and text coloring
    By amartino44 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-19-2013, 05:54 PM
  2. [SOLVED] Combining the Contents of 3 cells into 1 (with a twist)
    By Michael D in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-12-2013, 06:02 AM
  3. Replies: 2
    Last Post: 06-22-2011, 06:50 PM
  4. Coloring Cells dependent upon their contents
    By omgeokid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-16-2011, 02:33 PM
  5. Replies: 2
    Last Post: 08-22-2006, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1