Forum Discussion
Excel VBA to merge cell with different font style
To merge cells with different font styles while preserving the formatting in Excel VBA, you can use the following approach:
Sub MergeCellsAndPreserveFormatting()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
' Set the worksheet to the appropriate one
Set ws = ThisWorkbook.Sheets("Generated")
' Find the last row in Column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Loop through rows
For i = lastRow To 3 Step -1
If ws.Cells(i, 1).MergeCells Then
' Check if the cell in Column A is part of a merged cell
' Copy the content of the merged cell
ws.Cells(i, 8).Value = ws.Cells(i, 8).MergeArea(1, 1).Value
ws.Cells(i, 9).Value = ws.Cells(i, 9).MergeArea(1, 1).Value
ws.Cells(i, 10).Value = ws.Cells(i, 10).MergeArea(1, 1).Value
ws.Cells(i, 11).Value = ws.Cells(i, 11).MergeArea(1, 1).Value
' Unmerge the cell
ws.Cells(i, 1).MergeArea.UnMerge
End If
Next i
End SubThe code serves as an example, changes or adjustments may be necessary.
This VBA code loops through the rows and checks if the cell in Column A is part of a merged cell. If it is, it copies the content of the merged cell to the respective columns (H, I, J, K) while preserving the formatting. Then, it unmerges the cell in Column A.
Make sure to adjust the column references and worksheet name to match your specific setup. Also, be cautious and back up your data before running macros, especially when modifying cell contents. The text and steps were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you
Was the answer useful? Mark them as helpful!
This will help all forum participants.