Results 1 to 10 of 12

Thread: Concatenate with style

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    This slightly modified version will help get over the issue of different character formats within a cell.
    In this version, only cells with formulas are subject to having that formula stored, replaced with values , and then replaced after Rick's coding

    Code:
    Sub ConcatWithStyles2() '  https://www.mrexcel.com/board/threads/the-hardest-question-in-excel-history-a-very-smart-vba-macro-to-combine-cell-with-their-styles.808781/#post-3954687
    Rem 0a save any formulas, and replace with values
    Dim arrFormulas(1 To 5) As Variant
    Dim cnt
        For cnt = 1 To 5
            If Left(Cells.Item(1, cnt).Formula, 1) = "=" Then ' case a formula in cell
             Let arrFormulas(cnt) = Cells.Item(1, cnt).Formula
             Let Cells.Item(1, cnt).Value = Cells.Item(1, cnt).Value
            Else
            ' we don't have a formula , so we do nothing to the cell
            End If
        Next cnt
      Dim X As Long, Cell As Range, Text As String, Position As Long
      Range("A3").Value = Space(Evaluate("SUM(LEN(A1:F1))+COLUMNS(A1:F1)-1"))
      Position = 1
    '  Application.ScreenUpdating = False
      For Each Cell In Range("A1:F1")
        With Range("A3")
          .Characters(Position, Len(Cell.Value)).Text = Cell.Characters(1, Len(Cell.Value)).Text
          For X = 1 To Len(Cell.Value)
            With .Characters(Position + X - 1, 1).Font
              .Name = Cell.Characters(X, 1).Font.Name
              .Size = Cell.Characters(X, 1).Font.Size
              .Bold = Cell.Characters(X, 1).Font.Bold
              .Italic = Cell.Characters(X, 1).Font.Italic
              .Underline = Cell.Characters(X, 1).Font.Underline
              .Color = Cell.Characters(X, 1).Font.Color
              .Strikethrough = Cell.Characters(X, 1).Font.Strikethrough
              .Subscript = Cell.Characters(X, 1).Font.Subscript
              .Superscript = Cell.Characters(X, 1).Font.Superscript
              .TintAndShade = Cell.Characters(X, 1).Font.TintAndShade
              .FontStyle = Cell.Characters(X, 1).Font.FontStyle
            End With
          Next
        End With
        Position = Position + Len(Cell.Value) + 1
      Next
      Application.ScreenUpdating = True
    Rem 0b Put the formulas back
        For cnt = 1 To 5
            If arrFormulas(cnt) <> "" Then ' case a formula was in cell
             Let Cells.Item(1, cnt).Formula = arrFormulas(cnt)
            
            Else
            ' we didnt have a formula , so we do nothing to the cell
            End If
        Next cnt
    
    End Sub
    
    Before:
    http://i.imgur.com/gqW322Y.jpg

    After running above macro:
    http://i.imgur.com/792PzQY.jpg
    Last edited by DocAElstein; 12-08-2020 at 06:03 PM.

Similar Threads

  1. Yet Another Number-To-Words Function (Sorry, US Style Only)
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 10
    Last Post: 08-06-2020, 02:44 PM
  2. changing arrangement of data to new style
    By saied in forum Excel Help
    Replies: 3
    Last Post: 02-12-2015, 10:34 PM
  3. New Forum Style
    By Admin in forum Public News
    Replies: 2
    Last Post: 05-16-2014, 11:34 AM
  4. Replies: 6
    Last Post: 12-23-2013, 04:07 PM
  5. Excel Number Format: Indian Style Comma Separation
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 6
    Last Post: 09-18-2013, 11:38 AM

Posting Permissions

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