Concatenate with style

Abdulkhadar

Board Regular
Joined
Nov 10, 2013
Messages
165
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello Excel Genius,
Below VBA was posted by Mr Rick Rothstein
I want some change in this below VB. If any cell from A to F contains a formula like =Sheet1!A5&" "&Sheet1!C7 or =Sheet1!A5*3 etc it shows Runtime error '1004. How to solve this error.

VBA Code:
Sub ConcatWithStyles()
  Dim X As Long, LastRow As Long, LastCol As String, Cell As Range, Position As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Application.ScreenUpdating = False
  For Each Cell In Range("A1:F" & LastRow)
    LastCol = Split(Cells(Cell.Row, "G").End(xlToLeft).Address, "$")(1)
    If Cell.Column <= Columns(LastCol).Column Then
      With Range("H1").Offset(Cell.Row - 1)
        If Cell.Column = 1 Then
          .ClearFormats
          Position = 1
          Range("H" & .Row).Value = Space(Evaluate(Replace("SUM(LEN(A#:" & LastCol & _
                                    "#))+COLUMNS(A#:" & LastCol & "#)-1", "#", .Row)))
        End If
        .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
            .TintAndShade = Cell.Characters(X, 1).Font.TintAndShade
            .FontStyle = Cell.Characters(X, 1).Font.FontStyle
            .Superscript = Cell.Characters(X, 1).Font.Superscript
            .Subscript = Cell.Characters(X, 1).Font.Subscript
          End With
        Next
      End With
      Position = Position + Len(Cell.Value) + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub

Thanks in advance
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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