Macro Help! Maintaining font colours

CDel

New Member
Joined
Nov 25, 2021
Messages
4
Office Version
  1. 365
Platform
  1. MacOS
Hi, I have a macro that prompts a user to select cells - these can be non adjacent - and paste them into a cell the user selects.
I found the macro somewhere online and it's great.

I am looking to add in font colour.
The cells being copied from are specific colours and I need to be able to maintain colour in the pasted cell.
Any help would be greatly appreciated! Thanks

VBA Code:
Sub G()

    Dim strFinal$
    Dim cell As Range
    Dim rngSource As Range
    Dim rngArea As Range
    Dim rngTarget As Range

    Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
    Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
    For Each rngArea In rngSource
        For Each cell In rngArea
            strFinal = strFinal & cell.Value & " "
        Next
    Next
    strFinal = Left$(strFinal, Len(strFinal) - 1)
    rngTarget.Value = strFinal

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Assuming the cells selected to "merge" all have the same fill color:
VBA Code:
Sub G()
    Dim strFinal$
    Dim cell As Range, cellColor As Long
    Dim rngSource As Range
    Dim rngArea As Range
    Dim rngTarget As Range

    Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
    Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
    cellColor = rngSource(1).Interior.Color
    For Each rngArea In rngSource
        For Each cell In rngArea
            strFinal = strFinal & cell.Value & " "
        Next
    Next
    strFinal = Left$(strFinal, Len(strFinal) - 1)
    With rngTarget
        .Value = strFinal
        .Interior.Color = cellColor
        .EntireColumn.AutoFit
    End With
End Sub
 
Upvote 0
Give this macro a try...
VBA Code:
Sub CombineMultipleCellsIntoOneCellKeepColors()

  Dim strFinal As String
  Dim rngSource As Range, rngTarget As Range, Cell As Range
  
  Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
  Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
  
  For Each Cell In rngSource
    With rngTarget.Characters(Len(rngTarget))
      .Text = IIf(Len(rngTarget.Value), " ", "") & Cell.Value
      .Font.Color = Cell.Font.Color
    End With
  Next
  
End Sub
 
Upvote 0
Solution
Give this macro a try...
VBA Code:
Sub CombineMultipleCellsIntoOneCellKeepColors()

  Dim strFinal As String
  Dim rngSource As Range, rngTarget As Range, Cell As Range
 
  Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
  Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
 
  For Each Cell In rngSource
    With rngTarget.Characters(Len(rngTarget))
      .Text = IIf(Len(rngTarget.Value), " ", "") & Cell.Value
      .Font.Color = Cell.Font.Color
    End With
  Next
 
End Sub
Hi, Thank you very much for your response.

I have just tried this - for some reason, the last character in the first cell I choose to merge is is being cut off? I've attached an image to show what I mean. any ideas why this is? Otherwise it is brilliant!
 

Attachments

  • Screenshot 2021-11-25 at 17.26.43.png
    Screenshot 2021-11-25 at 17.26.43.png
    11.3 KB · Views: 6
Upvote 0
Give this macro a try...
VBA Code:
Sub CombineMultipleCellsIntoOneCellKeepColors()

  Dim strFinal As String
  Dim rngSource As Range, rngTarget As Range, Cell As Range
 
  Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
  Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
 
  For Each Cell In rngSource
    With rngTarget.Characters(Len(rngTarget))
      .Text = IIf(Len(rngTarget.Value), " ", "") & Cell.Value
      .Font.Color = Cell.Font.Color
    End With
  Next
 
End
[/QUOTE]

Assuming the cells selected to "merge" all have the same fill color:
VBA Code:
Sub G()
    Dim strFinal$
    Dim cell As Range, cellColor As Long
    Dim rngSource As Range
    Dim rngArea As Range
    Dim rngTarget As Range

    Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
    Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
    cellColor = rngSource(1).Interior.Color
    For Each rngArea In rngSource
        For Each cell In rngArea
            strFinal = strFinal & cell.Value & " "
        Next
    Next
    strFinal = Left$(strFinal, Len(strFinal) - 1)
    With rngTarget
        .Value = strFinal
        .Interior.Color = cellColor
        .EntireColumn.AutoFit
    End With
End Sub
Managed to get it working with @Rick Rothstein's Macro - but thank you for taking the time to answer - I really appreciate it!
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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