+ Reply to Thread
Results 1 to 7 of 7

Need VBA code to concatenate and preserve formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    04-14-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Need VBA code to concatenate and preserve formatting

    Could anyone show me some code to automatically concatenate a range of cells whilst preserving the original formatting?

    My document consists of around 200 columns and 16000 rows. Each row needs to be concatenated into column A with a "/" delimiter and preserve the original font (around 20 columns have red font which needs to be included in the destination cell).

    Many thanks

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Need VBA code to concatenate and preserve formatting

    Lexic0n,

    Welcome to the Excel Forum.

    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.

    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

    To attach your workbook, scroll down and click on the Go Advanced button, then scroll down and click on the Manage Attachments button.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Registered User
    Join Date
    04-14-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need VBA code to concatenate and preserve formatting

    Quote Originally Posted by stanleydgromjr View Post
    Lexic0n,

    Welcome to the Excel Forum.

    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.

    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

    To attach your workbook, scroll down and click on the Go Advanced button, then scroll down and click on the Manage Attachments button.

    I've now attached a cut-down example of what I'm trying to do (my original data set in 16000 rows x 200 columns).
    Attached Files Attached Files
    Last edited by Lexic0n; 05-17-2012 at 10:30 AM.

  4. #4
    Registered User
    Join Date
    04-14-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need VBA code to concatenate and preserve formatting

    If this helps, here is some code that I picked up previously and was trying to adapt for my needs (Credit to Erik Van Geit from Mr.Excel boards).


    Option Explicit
    
    Sub test()
    Call concatenate_cells_formats(Range("A1"), Range("B1:GI1"))
    End Sub
    
    Sub concatenate_cells_formats(Cell As Range, source As Range)
    
    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

    How can I make this work automatically for all 16000 rows in my document and include the "/" delimiter?

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Need VBA code to concatenate and preserve formatting

    Here's your code modified.

    Sub test()
        Dim cell   As Range
        Application.ScreenUpdating = False
        For Each cell In Range("B2", Range("B" & Rows.Count).End(xlUp))
            Call concatenate_cells_formats(cell.Offset(, -1), cell.Resize(, 5)) 'Destination column A, Source B:F
        Next cell
        Application.ScreenUpdating = True
    End Sub
    
    Sub concatenate_cells_formats(cell As Range, source As Range)
    
        Dim c      As Range
        Dim i      As Integer
    
        i = 1
    
        With cell
        
            .Value = vbNullString
            .ClearFormats
    
            For Each c In source
                If Len(c.Value) Then .Value = .Value & "/" & Trim(c)
            Next c
            
            .Value = Trim(Mid(.Value, 2))
    
            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) + 1, Length:=1).Font.Size = 1
                i = i + Len(Trim(c)) + 1
    
            Next c
    
        End With
    
    End Sub

  6. #6
    Registered User
    Join Date
    04-14-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need VBA code to concatenate and preserve formatting

    Thank you very much AlphaFrog, that works perfectly.

  7. #7
    Registered User
    Join Date
    02-23-2023
    Location
    Malaysia
    MS-Off Ver
    2016
    Posts
    1

    Re: Need VBA code to concatenate and preserve formatting

    Hi, LexicOn,

    Could you please provide the sample file with the correct VBA code for reference.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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