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
158
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?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,176
Members
448,554
Latest member
Gleisner2

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