Richard in Redditch

New Member
Joined
Feb 7, 2012
Messages
44
Hello.
I am sure this problem has come up before, but as a tyro I just don't know where my code is going wrong. I an trying to set up a macro, which will copy some data from a summary sheet into another sheet in the same workbook. Here is the code which is giving me angst:
Rich (BB code):
' Add the new sheet
    
    ActiveWorkbook.Sheets.Add After:=Sheets(LastShtNo)
   
' Copy heading and formatting to new sheet
    Sheets(LastShtNo).Range("2:2").Copy Sheets(NewShtNo).Range("2:2")
    Sheets(LastShtNo).Range("D4:G36").Copy Sheets(NewShtNo).Range("d4:g36")
    Sheets(LastShtNo).Range("c36").Copy Sheets(NewShtNo).Range("c36")
    Range("d4:e35").ClearContents
    Sheets(NewShtNo).Range("2:2").Select
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
' Get data
The system (Xl 2013) falls over at the underlined line with error "1004: Select error of Range class failed".

Assistance would be greatly appreciated!
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can't select a range unless it's sheet is active, but I can't see a need to select the range there. Just use:

Code:
Sheets(NewShtNo).Range("A2").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
 
Upvote 0
Hello Rory. Many thanks for your prompt reply.

Unfortunately, when I tried your modification, Excel fell over at the same place with the error "PasteSpecial method of Range class failed". Where am I going wrong?

Regards
 
Upvote 0
Try
Code:
    Sheets(LastShtNo).Range("2:2").Copy Sheets(NewShtNo).Range("2:2")
    Sheets(LastShtNo).Range("2:2").Copy
    Sheets(NewShtNo).Range("2:2").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Sheets(LastShtNo).Range("D4:G36").Copy Sheets(NewShtNo).Range("d4:g36")
    Sheets(LastShtNo).Range("c36").Copy Sheets(NewShtNo).Range("c36")
    Range("d4:e35").ClearContents
 
Upvote 0
Hello Rory and Fluff

Many thanks again for your help with my problem, which - I am very happy to say - is no longer a problem! Thank you Fluff - your solution has done the trick.

Btw, just why should basically reordering the order of the statements cause so much heartache? I had started on that piece of code about lunchtime and tried (almost) everything until I made the first posting. Is it an Excel "feature" - i.e. bug - or what? I could not find any reference in my VBA books.

Once again, sincerest thanks.
 
Upvote 0
Glad we could help & thanks for the feedback.

I suspect the reason it wasn't working, is that the last thing you copied was C36 (a single cell), but you then tried to pastespecial on the entire row 2, which means your copy & paste ranges were different sizes.
 
Upvote 0
I had amended that to only paste to the first cell, so I suspect the problem (which I overlooked) was that the ClearContents line was clearing the clipboard.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

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