By using expertatexcel.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

expertatexcel.com

 


MergeCells

Select a cell and merge it with the below cell if they have the same value - formatting.

Merge is a formatting technique to improve the appearance of the output with a loss of functionality.

A common reason to merge is to eliminate duplicate values from a report.

for example the following data

Projectdeploy idDatechangeStatus
Time tracking23323/22/2021Add new moduleActive
Time tracking23323/22/2021Add new TimerActive



Can be made better to view by merging the duplicates





Project deploy id Date change Status
Time tracking 2332 3/22/2021 Add new module Active
Add new Timer





An Un-do isn't supported so save before running


Sub MergeCells()

' activcell is used and one below
c1 = Activecell.Address
c2 = Activecell.Offset(1, 0).address

If Range(c1).value = Range(c2).value then
    Range(c1 & ":" & c2).Select
    With Selection ' recorded code to merge
        . code goes here
    End Width
    Application.DisplayAlerts = False
    Selection.Merge
    Application.DisplayAlerts = True
End If

ActiveCell.Offset(0, 1).select

End Sub



You can determine whether the Activecell is merged or not via

debug.Print Activecell.MergeCells

You can unmerge via

Activecell.MergCells = False