+ Reply to Thread
Results 1 to 22 of 22

macro to merge values from one column into one cell and retain source formatting.

  1. #1
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    macro to merge values from one column into one cell and retain source formatting.

    I need to compile data from multiple cells from one column into a single cell. Some of the source cells are formatted with as underlined text. I need to retain the formatting after the cells are merged into the destination cell. I found VBA to combine two ranges of cells and retain the source formatting, and it was more than I needed. How can I use just one range of cells and combine them? I don't want spaces or commas between the output values (I'll provide them when needed).

    Example:

    Source:

    A1= "It is going to cost "
    A2= "$1000.00" (A2 is formatted to underline value)

    Destination: (desired result)

    B2= "It is going to cost $1000.00" (A2 value is still underlined)


    My plan is to custom build each cell column A so that a macro can string the results together in one cell formatted as a paragraph with source formatting retained. I could use the concatenate function except it loses the source character formatting.

    Please help gently...very new to VBA

  2. #2
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: You try it with this macro

    PHP Code: 
    Option Explicit
    Sub ScottTaylor
    ()
     
    Dim Rng As RangeCls As Range
     
     Set Rng 
    Columns("A:A").SpecialCells(xlCellTypeConstants1)
     For 
    Each Cls In Rng
        Cls
    .Select
        Selection
    .AutoFill Destination:=Range(ClsCls.Offset(, 1)), Type:=xlFillDefault
        Cls
    .Offset(, 1).Value Cls.Offset(-1).Value CStr(Cls.Value)
     
    Next Cls
    End Sub 

  3. #3
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    I can't get the code to do anything. I need instructions on how to implement it once the macro is in a module.

  4. #4
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: You try it with this macro

    Quote Originally Posted by Sa DQ View Post
    PHP Code: 
    Option Explicit
    Sub ScottTaylor
    ()
     
    Dim Rng As RangeCls As Range
     
     Set Rng 
    Columns("A:A").SpecialCells(xlCellTypeConstants1)
     For 
    Each Cls In Rng
        Cls
    .Select
        Selection
    .AutoFill Destination:=Range(ClsCls.Offset(, 1)), Type:=xlFillDefault
        Cls
    .Offset(, 1).Value Cls.Offset(-1).Value CStr(Cls.Value)
     
    Next Cls
    End Sub 
    I have no VBA experience, so I need to have step by step implementation instructions. How do I specify the souce cells I want to combine and how do I specify the destination cell? Thanks so much for the help

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

    Re: macro to merge values from one column into one cell and retain source formatting.

    Scott Taylor,

    Thank you for the two Private Messages.


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
    2. Open your workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    Please Login or Register  to view this content.

    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


    Then run the ScottTaylor macro.
    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.

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

    Re: macro to merge values from one column into one cell and retain source formatting.

    Scott Taylor,

    In the future:

    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.

  7. #7
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    Stan,

    Thanks for the code. Unfortunately it appears to be the same code I already got. I need to be able to specify which worksheet within the workbook to run the macro and I also need to know how to specify the source cells and the destination cell. I need to understand the syntax of the macro details in order to change or customize the macro. My workbook has more than 15 worksheets and they all have conditional formulas scattered throughout. The particular worksheet that needs this macro is a document that must ultimately be printed as a pdf document with precise language, spacing, fonts, etc. to duplicate another document. Because the text of the document must be justified and comes from several of the other worksheets within the workbook I found that a single column should contain all the separate pre-formatted sections of the paragraph(destination cell).

    I haven't finished composing the sheet that I want to use the macro with, but I have attached a much simplified version of a workbook that may make things more clear.

    Thank you very much for your help.release-trial (Autosaved).xlsm

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: macro to merge values from one column into one cell and retain source formatting.

    This should be just enough for your need.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    release-trial (Autosaved) With Code.xlsmrelease-trial (Autosaved).xlsm

    Stan,

    Its almost what I need. However, if I change the location of the starting cell and the corresponding range references the underlining is off by one space.

    If I move the starting cell of the source range down to a higher cell number, i.e. A117, the cells change all the formatting to odd places, i.e, italics and underlining starts in new spots.

    I need to lock the formatting to the exact location of the source formatting. Cell A1 will not ultimately be where I start the range...it will be more like A117 and continue on through A185 or so.

    Because the output must perfectly match a benchmark document I also need to control the spacing by putting spaces in the source cells and have the macro note the spacing without adding its ownspacing. I observed that in the output the parenthesis were separated by a space between the underlined 12 text. I need to be able to bring the parenthesis right next to the number without underlining the parenthesis. That's why I suggest I need to control the spacing from the source cell side.

    Thanks for all the amazing help. I'm much farther along than I was two days ago.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: macro to merge values from one column into one cell and retain source formatting.

    Who are you talking to?

    Change to
    1)
    Please Login or Register  to view this content.
    2)
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    Sorry,

    Got confused with all the great help, I wasn't sure who sent the help. I'm going to try the change suggested. Thanks!

  12. #12
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    WOW....I'm just about there! Thanks again. One last issue....can we lose the autospacing between words and let me dictate the spacing in the source cells?

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: macro to merge values from one column into one cell and retain source formatting.

    Sure.
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    Fantastic! Thank you so much Jindon! Where do I meet you to buy dinner?

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: macro to merge values from one column into one cell and retain source formatting.

    Thanks for the dinner and don't forget to mark the thread as "Solved".

  16. #16
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    Jindon....One more heroic gesture? I need the macro to only run on the sheet named "Release" I have over 20 sheets in the workbook and I can't have it running on other sheets.

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: macro to merge values from one column into one cell and retain source formatting.

    1) Change
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.
    2) Change
    Please Login or Register  to view this content.
    To
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    You are scary smart.

  19. #19
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    I've finally got the worksheet partially built. The code provided is working great, but I need (A11) to automatically adjust row height to fit the text. Is there a place to insert a bit of code in this macro?

    Option Explicit

    Sub Release_Combine_Text()
    Dim rng As Range, r As Range, txt As String, Temp As Long
    With Sheets("Release")
    Set rng = Range("A117", Range("A" & Rows.Count).End(xlUp))
    End With
    For Each r In rng
    txt = txt & r.Text
    Next
    With rng.Parent.Range("a11")
    .Value = txt
    For Each r In rng
    With .Characters(Temp + 1, Len(r.Text)).Font
    .FontStyle = r.Font.FontStyle
    .Size = r.Font.Size
    .Name = r.Font.Name
    .Color = r.Font.Color
    .Strikethrough = r.Font.Strikethrough
    .Subscript = r.Font.Subscript
    .Superscript = r.Font.Superscript
    .Underline = r.Font.Underline
    End With
    Temp = Temp + Len(r.Text)
    Next
    End With
    End Sub

  20. #20
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    I've finally got the worksheet partially built. The code provided is working great, but I need (A11) to automatically adjust row height to fit the text. Is there a place to insert a bit of code in this macro?

    Option Explicit

    Sub Release_Combine_Text()
    Dim rng As Range, r As Range, txt As String, Temp As Long
    With Sheets("Release")
    Set rng = Range("A117", Range("A" & Rows.Count).End(xlUp))
    End With
    For Each r In rng
    txt = txt & r.Text
    Next
    With rng.Parent.Range("a11")
    .Value = txt
    For Each r In rng
    With .Characters(Temp + 1, Len(r.Text)).Font
    .FontStyle = r.Font.FontStyle
    .Size = r.Font.Size
    .Name = r.Font.Name
    .Color = r.Font.Color
    .Strikethrough = r.Font.Strikethrough
    .Subscript = r.Font.Subscript
    .Superscript = r.Font.Superscript
    .Underline = r.Font.Underline
    End With
    Temp = Temp + Len(r.Text)
    Next
    End With
    End Sub

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: macro to merge values from one column into one cell and retain source formatting.

    You need to edit you post and wrap the code with code tag first.
    It's a "MUST" rules here.
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]

  22. #22
    Registered User
    Join Date
    07-05-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: macro to merge values from one column into one cell and retain source formatting.

    I've finally got the worksheet partially built. The code provided is working great, but I need (A11) to automatically adjust row height to fit the text. Is there a place to insert a bit of code in this macro?

    Please Login or Register  to view this content.

+ 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