Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Copy merged cells making target also a merged cell

I have a merged cells A1:B1
I would like to copy that merged cell, however i would like to have a source cell merged in the same way.

Sub Copy()

Sheet2.Range("A1").Copy
Sheet2.Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub

Using this code I will paste value of A1 to A5, however A5 wont be merged with B5, and that is the result I am looking for.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can specifically tell the destination cell to become merged. And you can specify the merge area by counting the rows and columns of the source cell’s merged area.

Sub Example()
    Dim Src As Range
    Set Src = Range("A1")
    
    Dim Dst As Range
    Set Dst = Range("A5")
    
    Dst.Value = Src.Value
    
    Dst.Resize(Src.MergeArea.Rows.Count, Src.MergeArea.Columns.Count).Merge
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading