PasteSpecial Method of Range Class Failed

John.McLaughlin

Board Regular
Joined
Jul 19, 2011
Messages
169
Hello, would conditional formatting of the destination cells cause this error?

Thanks in advance!

Code:
Sub Post()

    Application.CutCopyMode = False
    Dim DstRng As Range
    Dim DstWks As Worksheet
    Dim LastRow As Long
    Dim N As Long, r As Long
    Dim SrcRng As Range
    Dim SrcWks As Worksheet


    Sheets("SHEET2").Visible = True
    Sheets("SHEET1").Visible = True
    Sheets("SHEET1").Select
    ActiveSheet.Unprotect
     

    
      ' Assign the Worksheets
        Set SrcWks = Worksheets("SHEET2")
        Set DstWks = Worksheets("SHEET1")
      

      
      ' Get all cells in the Source Range starting with row 5
        Set SrcRng = SrcWks.Range("A5:E5")
        LastRow = SrcWks.Cells(Rows.Count, "A").End(xlUp).Row
        If LastRow < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Resize(LastRow - SrcRng.Row + 1, 5)
        
      
      ' Find the next empty row in the Destination Range starting at row 5
        
        Set DstRng = DstWks.Range("B5")
        LastRow = DstWks.Cells(Rows.Count, "B").End(xlUp).Row
        Set DstRng = IIf(LastRow < DstRng.Row, DstRng, DstRng.Offset(LastRow - DstRng.Row + 1, 0))

        
        ' This is the paste of the text
          DstRng.Select

        ' This is the line that stops the code
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

        
 Sheets("SHEET2").Select
   Range("B5").Select

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi John. Thanks for posting on the forum.

The important thing is missing from your code: Copy

I also made some adjustments to the code, try the following:

VBA Code:
Sub Post()
  Dim DstWks As Worksheet, SrcWks As Worksheet
  Dim DstRng As Range, SrcRng As Range
  Dim LastRow As Long
 
  ' Assign the Worksheets
  Set SrcWks = Sheets("SHEET2")
  Set DstWks = Sheets("SHEET1")
 
  SrcWks.Visible = True
  DstWks.Visible = True
  DstWks.Unprotect
 
  ' Get all cells in the Source Range starting with row 5
  Set SrcRng = SrcWks.Range("A5:E5")
  LastRow = SrcWks.Cells(Rows.Count, "A").End(xlUp).Row
  If LastRow < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Resize(LastRow - SrcRng.Row + 1, 5)
 
  ' Find the next empty row in the Destination Range starting at row 5
  Set DstRng = DstWks.Range("B5")
  LastRow = DstWks.Cells(Rows.Count, "B").End(xlUp).Row
  Set DstRng = IIf(LastRow < DstRng.Row, DstRng, DstRng.Offset(LastRow - DstRng.Row + 1, 0))
 
  'copy
  SrcRng.Copy
  DstRng.PasteSpecial Paste:=xlPasteValues
 
  SrcWks.Select
  Range("B5").Select
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Solution
Hi John. Thanks for posting on the forum.

The important thing is missing from your code: Copy

I also made some adjustments to the code, try the following:

VBA Code:
Sub Post()
  Dim DstWks As Worksheet, SrcWks As Worksheet
  Dim DstRng As Range, SrcRng As Range
  Dim LastRow As Long
 
  ' Assign the Worksheets
  Set SrcWks = Sheets("SHEET2")
  Set DstWks = Sheets("SHEET1")
 
  SrcWks.Visible = True
  DstWks.Visible = True
  DstWks.Unprotect
 
  ' Get all cells in the Source Range starting with row 5
  Set SrcRng = SrcWks.Range("A5:E5")
  LastRow = SrcWks.Cells(Rows.Count, "A").End(xlUp).Row
  If LastRow < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Resize(LastRow - SrcRng.Row + 1, 5)
 
  ' Find the next empty row in the Destination Range starting at row 5
  Set DstRng = DstWks.Range("B5")
  LastRow = DstWks.Cells(Rows.Count, "B").End(xlUp).Row
  Set DstRng = IIf(LastRow < DstRng.Row, DstRng, DstRng.Offset(LastRow - DstRng.Row + 1, 0))
 
  'copy
  SrcRng.Copy
  DstRng.PasteSpecial Paste:=xlPasteValues
 
  SrcWks.Select
  Range("B5").Select
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------

Forgive me @Dante Amor,

When I change the Sheet Names in your reply to it's actual name, the copy doesn't happen?

If I use the CodeNames (Sheet2 & Sheet41), I get Subscript out of range. I tried to qualify the sheet names when using the Codenames, and same Subscript out of range error?

Your code was beautiful! I have spent the last few days trying to learn the answer :( Why would something so simple as a sheet name change do this?

Thanks (again) in advance!


Code:
Sub Post()
  
  
  Dim wbThis As Workbook
  Set wbThis = ActiveWorkbook
  wbThis.Activate
 
 
  Dim DstWks As Worksheet, SrcWks As Worksheet
  Dim DstRng As Range, SrcRng As Range
  Dim LastRow As Long
 
 
  With ThisWorkbook
 
  ' Assign the Worksheets
  Set SrcWks = wbThis.Sheets("SHEET41")
  Set DstWks = wbThis.Sheets("SHEET2")
 
  End With
  
  
  SrcWks.Visible = True
  DstWks.Visible = True
  DstWks.Unprotect
 
  ' Get all cells in the Source Range starting with row 5
  Set SrcRng = SrcWks.Range("A5:E5")
  LastRow = SrcWks.Cells(Rows.Count, "A").End(xlUp).Row
  If LastRow < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Resize(LastRow - SrcRng.Row + 1, 5)
 
  ' Find the next empty row in the Destination Range starting at row 5
  Set DstRng = DstWks.Range("B5")
  LastRow = DstWks.Cells(Rows.Count, "B").End(xlUp).Row
  Set DstRng = IIf(LastRow < DstRng.Row, DstRng, DstRng.Offset(LastRow - DstRng.Row + 1, 0))
 
  'copy
  SrcRng.Copy
  DstRng.PasteSpecial Paste:=xlPasteValues
 
  DstWks.Select
  Range("B5").Select
End Sub
 
Upvote 0
If I use the CodeNames (Sheet2 & Sheet41)
The codename is already the name of the object.

The sheet have 3 names (so to speak):
1) The name of the tab.
2) The codename (name of the object).
3) The index of the sheet.

I think it is better explained in the following image:
1682002006508.png

VBA Code:
Sub Explanation_1()
  Dim sh As Worksheet
  
  Set sh = Sheets("Master")   'Reference to the tab name in quotes
  Set sh = Sheet1             'Reference to object
  Set sh = Sheets(1)          'Reference to index
End Sub
------------------------------------------------------------------


So the code, if you want to use the codename:
VBA Code:
Sub Post()
  Dim DstWks As Worksheet, SrcWks As Worksheet
  Dim DstRng As Range, SrcRng As Range
  Dim LastRow As Long
 
  ' Assign the Worksheets
  Set SrcWks = Sheet41
  Set DstWks = Sheet2
 
  SrcWks.Visible = True
  DstWks.Visible = True
  DstWks.Unprotect
If you want to reference Thisworkbook:
Rich (BB code):
Sub Post()
  Dim wb As Workbook
  Dim DstWks As Worksheet, SrcWks As Worksheet
  Dim DstRng As Range, SrcRng As Range
  Dim LastRow As Long
 
  Set wb = ThisWorkbook
  ' Assign the Worksheets
  Set SrcWks = wb.Sheet41
  Set DstWks = wb.Sheet2
 
  SrcWks.Visible = True
  DstWks.Visible = True
  DstWks.Unprotect
Or this:
Rich (BB code):
Sub Post_v2()
  Dim DstWks As Worksheet, SrcWks As Worksheet
  Dim DstRng As Range, SrcRng As Range
  Dim LastRow As Long
 
  ' Assign the Worksheets
  With ThisWorkbook
    Set SrcWks = .Sheet41
    Set DstWks = .Sheet2
  End With
 
  SrcWks.Visible = True
  DstWks.Visible = True
  DstWks.Unprotect

I hope the above helps you understand a bit more how to reference sheets and the workbook.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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