Help please - Runtime error 1004 paste method of worksheet class failed

marcus314

New Member
Joined
Jun 4, 2014
Messages
15
Hello,
I am attempting to run the following macro, but I received a runtime error 1004 (paste method of worksheet class failed). This happens on the last ActiveSheet.Paste command. Could any of you point me in the right direction?
Thanks
Rich (BB code):
Sub Combine()


Worksheets("SourceSheet1").Activate


Range("A2").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy
Worksheets("DestinationSheet").Activate
Range("A2").Select
ActiveSheet.Paste


Worksheets("SourceSheet2").Activate
Range("A2").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Worksheets("DestinationSheet").Range("A65536").End(xlUp)(2)


Worksheets("DestinationSheet").Activate
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1).EntireRow.Select
ActiveSheet.Paste


End Sub
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,490
Office Version
  1. 2019
Platform
  1. Windows
Hi,
Not tested your code but one cause could be you have a protected worksheet(s)?

See if this update to your code helps. If sheets are protected add password if required

Code:
Sub Combine()
    Dim rng As Range
    Dim sh As Worksheet


    For Each sh In Worksheets(Array("SourceSheet1", "SourceSheet2"))
        sh.Unprotect Password:=""
        Set rng = sh.Range("A1").CurrentRegion
        Set rng = rng.Resize(rng.Rows.Count - 1)
        Set rng = rng.Offset(1, 0)
        With Worksheets("DestinationSheet")
            .Unprotect Password:=""
            rng.Copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
            'add protection if required
            '.Protect Password:=""
        End With
        'add protection if required
        'sh.Protect Password:=""
        Set rng = Nothing
    Next sh


End Sub

Hope Helpful

Dave
 

marcus314

New Member
Joined
Jun 4, 2014
Messages
15
Hi,
Not tested your code but one cause could be you have a protected worksheet(s)?

See if this update to your code helps. If sheets are protected add password if required

Code:
Sub Combine()
    Dim rng As Range
    Dim sh As Worksheet


    For Each sh In Worksheets(Array("SourceSheet1", "SourceSheet2"))
        sh.Unprotect Password:=""
        Set rng = sh.Range("A1").CurrentRegion
        Set rng = rng.Resize(rng.Rows.Count - 1)
        Set rng = rng.Offset(1, 0)
        With Worksheets("DestinationSheet")
            .Unprotect Password:=""
            rng.Copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
            'add protection if required
            '.Protect Password:=""
        End With
        'add protection if required
        'sh.Protect Password:=""
        Set rng = Nothing
    Next sh


End Sub

Hope Helpful

Dave
Thanks for the help, Dave. The records properly paste here, though it replaces what was previously pasted. Rather, I'm hoping to build upon what was pasted (i.e., combine the contents of the two separate worksheets into the destination sheet). Any ideas how to properly do so? Thanks again for agreeing to help!!
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,490
Office Version
  1. 2019
Platform
  1. Windows
Hi,
suggested code should add the data from each source sheet to destination sheet placing in the next empty row. Unless Column A is empty when data is copied to destination sheet, it should not overwrite (replace) existing data.

Dave
 

marcus314

New Member
Joined
Jun 4, 2014
Messages
15
Hi,
suggested code should add the data from each source sheet to destination sheet placing in the next empty row. Unless Column A is empty when data is copied to destination sheet, it should not overwrite (replace) existing data.

Dave
This is super helpful, Dave! And in the event Column A does have blank records, is there a workaround to still force copy? Thank you!!
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,490
Office Version
  1. 2019
Platform
  1. Windows
Try this change to code:

Code:
Sub Combine()
    Dim rng As Range
    Dim r As Long
    Dim sh As Worksheet


    For Each sh In Worksheets(Array("SourceSheet1", "SourceSheet2"))
        sh.Unprotect Password:=""
        Set rng = sh.Range("A1").CurrentRegion
        Set rng = rng.Resize(rng.Rows.Count - 1)
        Set rng = rng.Offset(1, 0)
        With Worksheets("DestinationSheet")
            .Unprotect Password:=""
            rng.Copy .Range("A" & NewRow(.Name) + 1)
            'add protection if required
            '.Protect Password:=""
        End With
        'add protection if required
        'sh.Protect Password:=""
        Set rng = Nothing
    Next sh


End Sub


Function NewRow(ByVal sh As String) As Long
    On Error Resume Next
    NewRow = Worksheets(sh).Cells.Find(What:="*", _
                            After:=Worksheets(sh).Range("A1"), _
                            Lookat:=xlPart, LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Functionl

Ensure that you copy Function code with updated Combine Code which can be placed in same module.

Dave
 

Watch MrExcel Video

Forum statistics

Threads
1,128,167
Messages
5,629,079
Members
416,364
Latest member
maatpsr

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
Top