Named ranges from multiple sections of cells copy paste not working

Caveman1964

Board Regular
Joined
Dec 14, 2017
Messages
127
Office Version
  1. 2016
Platform
  1. Windows
I named 4 different sections of cells as a range name,
specifically sheet 2 a1:a5, a10:a15 and a20:a25. named it "split"
I wanted to copy paste into same range on sheet 1 and named it "splittgt"
It gets all the way to paste then gives me this error.
"This action wont work on multiple selections"
Here is code, I have been working with this for an hour with no luck. I will continue to attempt to figure it out but would appreciate and be grateful for any help.
I am not real good with VBA.
Sub Rectangle2_Click()
Dim lastrow As Long

Dim split As Range
Dim splittgt As Range
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("sheet1")
Set ws2 = Sheets("sheet2")
Set wb = ThisWorkbook
Set split = ws2.Range("split")
Set splittgt = ws1.Range("splittgt")

split.Copy

splittgt.PasteSpecial Paste:=xlPasteValues

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
1) Is there content on the other cells e.g. A6:A9, A16:A19...?
Why not just copy A1:A25 all at once?
 
Upvote 0
1) Is there content on the other cells e.g. A6:A9, A16:A19...?
Why not just copy A1:A25 all at once?
Thanks for the question.
Yes there is other content that can't be disturbed. This is why I need to just take the blocks of info and paste into same blocks of other sheet.
 
Upvote 0
You can't have the same name for different ranges of cells. What range is assigned to "split"? Can you check in the Name Manager (alt + m + n) ?
 
Upvote 0
You can't have the same name for different ranges of cells. What range is assigned to "split"? Can you check in the Name Manager (alt + m + n) ?
split = ranges sheet 2 a1:a5, a10:a15 and a20:a25
 
Upvote 0
Try this on a copy.
VBA Code:
Sub CopyAndNameRange2()
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    Dim sourceRange As Range
    Dim targetRange As Range
    Dim sourceArea As Range
    Dim targetArea As Range
    Dim areaIndex As Integer
    
    ' Set the source and target worksheets
    Set wsSource = ThisWorkbook.Sheets("Sheet2")
    Set wsTarget = ThisWorkbook.Sheets("Sheet1")
    
    ' Get the range named "split" from the source worksheet
    On Error Resume Next
    Set sourceRange = wsSource.Range("split")
    On Error GoTo 0

    
    ' Loop through each area in the named range and copy its values to the target range
    For areaIndex = 1 To sourceRange.Areas.Count
        Set sourceArea = sourceRange.Areas(areaIndex)
        Set targetArea = wsTarget.Range(sourceArea.Address)
        
        If targetRange Is Nothing Then
            Set targetRange = targetArea
        Else
            Set targetRange = Union(targetRange, targetArea)
        End If
        
        targetArea.Value = sourceArea.Value
    Next areaIndex
    
    ' Name the target range as "splittgt"
    If Not targetRange Is Nothing Then
        targetRange.Name = "splittgt"
    End If
    
End Sub
 
Upvote 0
"This action wont work on multiple selections"
You can't copy non-contiguous ranges or you will get that error. Try it.
Select split from the name drop down box to select the non-contiguous cells and hit Ctrl+C.
You will immediately get that error message.

The code from @Cubist is probably more flexible but since your input and output ranges are identical you can probably get away with this.

VBA Code:
Sub Rectangle2_Click()
    Dim lastrow As Long
    
    Dim split As Range
    'Dim splittgt As Range                       ' Not used
    Dim wb As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Sheets("sheet1")
    Set ws2 = Sheets("sheet2")
    Set wb = ThisWorkbook
    Set split = ws2.Range("split")
    'Set splittgt = ws1.Range("splittgt")       '  Not used
    
    Dim rcell As Range
    
    For Each rcell In split.Cells
        With ws1
            .Range(rcell.Address).Value = rcell.Value
        End With
    Next rcell

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,083
Members
453,021
Latest member
Justyna P

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