Paste special error (method of range class failed)

kezwick

New Member
Joined
Aug 22, 2011
Messages
26
Hi All,

Having a head scratcher have the below macro and it works every other time! fill in the table press button and fails (debug points to Paste special line). after which i change nothing in the code and press button again runs fine! its almost like its losing the data from the clipbord? any help would be brilliant thanks again all!

Code:
Sub Macro1()
'
' Macro1 Macro
'
    Sheets("Input").Select
    lastCol = ActiveSheet.Range("a2").End(xlToRight).Column
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a2", ActiveSheet.Cells(lastRow, lastCol)).Select
    Selection.Copy
    Sheets("Data").Select
    ActiveSheet.Unprotect
    Range("G1").Select
    Range("G1").End(xlDown).Select
    ActiveCell.Offset(1, -6).Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Sheets("Input").Select
    Range("A2:F20").Select
    Selection.ClearContents
    Range("h2:j20").Select
    Selection.ClearContents
    ActiveWorkbook.Save
'
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi

Try this, looks like it was mostly to do with your sheet protection and the positioning of the protect & unprotect.

Code:
Sheets("Data").Unprotect
Sheets("Input").Activate
    lastCol = ActiveSheet.Range("a2").End(xlToRight).Column
lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a2", ActiveSheet.Cells(lastRow, lastCol)).Select
    Selection.Copy
    Sheets("Data").Activate
    
    ActiveSheet.Range("G1").Select
    ActiveSheet.Range("G1").End(xlDown).Select
    ActiveCell.Offset(1, -6).Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    Sheets("Input").Activate
    ActiveSheet.Range("A2:F20").Select
    Selection.ClearContents
    ActiveSheet.Range("h2:j20").Select
    Selection.ClearContents
    
    Sheets("Data").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
    'ActiveWorkbook.Save

Kev
 
Upvote 0
The macro recorder is a good way to learn VBA and can be useful in getting correct syntax even after you get more practice, but it records a lot of useless stuff as well.
You'll find as you get more expreience that it's better to be clearer exactly what objects you are dealing with.
This is a cleaner way of doing what your recorded code did.

Code:
Sub CleanerCode()
With Sheets("Input")
    Set Rng = Range(.Range("A2"), .UsedRange.Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count))
End With
With Sheets("Data")
    .Unprotect
    .Range("G1").End(xlDown).Offset(1, -6).Resize(Rng.Rows.Count, Rng.Columns.Count).Value = Rng.Value
    .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
Rng.ClearContents
End Sub

Hope this helps.

Teeroy.
 
Upvote 0
Brilliant that works Teeroy! many thanks for all the help still trying to get my head around it but will get it one day (he says!!!)
Thanks again to both for the help much appreciated!
 
Upvote 0
You're welcome.

Just keep practising; that's what we all do :).
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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