VBA to copy and paste text from merged cells in a WB to merged cells in another

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
146
I know working with merged cells can be problematic. I have a workbook and I am trying to copy the text (which will change from month to month) within the merged cells and paste the text into merged cells of another workbook. Of course, they are not the same size to due presentation reasons.

Is there a way to do this? If not, any suggestions?
 

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,733
You can copy the contents from one group of merged cells to the other, if you know the source and destination cells, or if each workbook only contains a single merge area, that area can be found and the contents transferred. This will not change the target area, so text layout may be different than when it was in its source depending on the amount of text copied and the formatting of the source & destination cells.

If you used names for the merged areas, this would be one way to identify by content rather than by location.

Code:
Option Explicit

Sub CopyFromMergedAreaToMergedArea()
    
    Range("K9").MergeArea.Copy Destination:=Range("V17").MergeArea

End Sub

Sub FindMergedCellsOnActiveSheet()

    Dim oFound As Object
    Dim sValue As String
    Dim lLwkCount As Long
    Dim lLWKIndex As Long
    Dim lPos As Long
    Dim lStartInstr As Long
    Dim sFirstAddress As String
    Dim sOutput As String
    Dim lMergeCount As Long

    Application.FindFormat.Clear
    Application.FindFormat.MergeCells = True
    With ActiveSheet.UsedRange
        .Cells(1, 1).Select  'Otherwise an error if activecell is outside usedrange
        
        On Error Resume Next 'Continue if no merged cells on activesheet
        .Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=True).Activate
            
        If Err.Number = 91 Then
            MsgBox "No merged cells on worksheet " & ActiveSheet.Name, , "No Merged Cells"
            GoTo End_Sub
        End If
        On Error GoTo 0
        
        sFirstAddress = Selection.Address
        Do
            sOutput = sOutput & ", " & Selection.Address
            lMergeCount = lMergeCount + 1
            'Normally would use FindNext here, but it would not work with only formatting
            .Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=True).Activate
        Loop While Selection.Address <> sFirstAddress
        
        sOutput = Mid(sOutput, 3)
        
        MsgBox sOutput, , lMergeCount & " Merged Area" & IIf(lMergeCount > 1, "s", "")
    End With
    
End_Sub:
    
End Sub
 

Forum statistics

Threads
1,085,489
Messages
5,383,957
Members
401,868
Latest member
herbalgirlskincare

Some videos you may like

This Week's Hot Topics

Top