tags:

views:

679

answers:

3

As the title says: I try to merge the contents of 2 cells into a 3rd in Excel. There was a similar question here on SO, but those solutions do not keep the character format intact. For example, parts of the source cell contents are formatted bold and red, other parts are normal. When I merge them like

 Range("A3") = Range("A1") & Range("A2")

then any formatting of A1 and A2 is lost. What I need is a solution keeping the format intact. This is going to be part of a bigger VBA program, so I need a VBA solution, no formula, please. Excel version is 2002(XP).

A: 

I know of no way of doing this directly, but it would not be that hard to copy the ranges to the new range then loop through the interior properties of each character of the original ranges and copy the properties over. That should preserve colour, font style etc. for each character in the resulting range.

Home time beckons so I've not got time to whip up an example, but I'm sure others edit/answer if need be.

Lunatik
+2  A: 

Doc, that is an interesting question. I was stumped myself but saw the value, so after some searching, here is what I found. From vbaexpress I got the basic understanding of in cell formatting, which I modified for your use below.

Sub Merge_Cells()
Dim iOS As Integer
Dim rngFrom1 As Range
Dim rngFrom2 As Range
Dim rngTo As Range
Dim lenFrom1 As Integer
Dim lenFrom2 As Integer

  Set rngFrom1 = Cells(1, 1)
  Set rngFrom2 = Cells(1, 2)
  Set rngTo = Cells(1, 3)
  lenFrom1 = rngFrom1.Characters.Count
  lenFrom2 = rngFrom2.Characters.Count

  rngTo.Value = rngFrom1.Text & rngFrom2.Text

  For iOS = 1 To lenFrom1
    With rngTo.Characters(iOS, 1).Font
      .Name = rngFrom1.Characters(iOS, 1).Font.Name
      .Bold = rngFrom1.Characters(iOS, 1).Font.Bold
      .Size = rngFrom1.Characters(iOS, 1).Font.Size
      .ColorIndex = rngFrom1.Characters(iOS, 1).Font.ColorIndex
    End With
  Next iOS
  For iOS = 1 To lenFrom2
    With rngTo.Characters(lenFrom1 + iOS, 1).Font
      .Name = rngFrom2.Characters(iOS, 1).Font.Name
      .Bold = rngFrom2.Characters(iOS, 1).Font.Bold
      .Size = rngFrom2.Characters(iOS, 1).Font.Size
      .ColorIndex = rngFrom2.Characters(iOS, 1).Font.ColorIndex
    End With
  Next iOS

End Sub

Just change out the 3 cells() with your specific cells. Maybe someone can find a cleaner way, but when I tested this, it worked as I understand you (and I) would like.
Hope this helps...

Craig
Thanks Craig, that answer is very similar to the solution I found myself in the last hour. There is one drawback: it is very slow (that means, for 200-300 characters, the user can see how the formatting takes place), so I hope someone else finds a better solution.
Doc Brown
How does the speed change if you add these at the beginning?application.screenupdating = falseapplication.enableevents = falseapplication.calculation = xlmanualThen at the endapplication.calculation = xlautomaticapplication.screenupdating = trueapplication.enableevents = true
guitarthrower
@guitarthrower: Great, that helps!! At least, the visible effect is gone, and that is what was looking for. I will accept Craigs answer tomorrow if no one finds a better solution.
Doc Brown
@Doc Brown: Glad I could help. A vote up on the comment would be appreciated if you thought it was helpful.
guitarthrower
+1  A: 

This is a bit of a shot in the dark, but if you could somehow write a macro that does the following, it seems like it would work beautifully:

  1. Copy the entire range (e.g., A1:B200) to be merged.
  2. Open a new Word document.
  3. Paste (creates a table in Word).
  4. Select the table.
  5. Do "Convert To Text" with a blank delimiter (or whatever you want).
  6. Copy the resulting text.
  7. Paste into desired location (e.g., C1) in Excel.

I know you can write VBA macros for both Excel and Word, but I have doubts that you could control a Word document from Excel. You probably could write a C# console app that could open and control two documents, however.

DanM
@DanM: in fact, the VBA program I am working on does control Word from Excel! I have no problems to copy the content of Excel cells to Word, keeping the formatting intact. The task is to let the user choose some (formatted!) boilerplates from a separate sheet, merge them together in one cell, replace some placeholder texts manually and afterwards (when finished with a hundred or so cells) generate a report in Word. For practical reasons, I would prefer a solution where Word is not started just for merging the boilerplates, only afterwards for the final report.
Doc Brown