Multiple Range (union) Copy-Paste

Renaissance

Board Regular
Joined
Jun 20, 2013
Messages
58
Greetings!

I am working on a simple macro that will copy a header row and other row within the same sheet and pasting it into a new sheet. This action repeats until all rows have been copied, however right now I am getting a "This action won't work on multiple selections" error '1004' and can't figure out why.

Here's the code

Code:
Sub Transpose()

    Dim cnt As Integer
    Dim i As Integer
    Dim clmns As Integer
    Dim range1 As Range
    Dim range2 As Range
    Dim myMultipleRange As Range
                
    Sheets("Sheet1").Activate
    clmns = Worksheets("Sheet1").Range("1:1").Cells.SpecialCells(xlCellTypeConstants).Count
    cnt = Worksheets("Sheet1").Range("B:B").Cells.SpecialCells(xlCellTypeConstants).Count
    Set range1 = Worksheets("Sheet1").Range("B1:B" & clmns)
        
    For i = 2 To cnt
        
        Set range2 = Sheets("Sheet1").Range(Cells(i, 2), Cells(i, clmns))
        Set myMultipleRange = Union(range1, range2)
        myMultipleRange.Select
        Selection.Copy
        Sheets.Add After:=ActiveSheet
        Range("B1").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
        Sheets("Sheet1").Select
    Next i

End Sub

I'm assuming it's something simple and I've eyeballed code from a few others, but I appreciate any advice you can offer :)

~Renaissance
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
Set range1 = Worksheets("Sheet1").Range("B1:B" & [COLOR="#FF0000"]clmns[/COLOR])

The clmns variable is a Columns count, but it's being used as a Row argument. I think you want something like this...
Code:
Set range1 = Worksheets("Sheet1").Range("B1").Resize(1, clmns -1)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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