Search code examples
excelvbacopy-paste

Unable to get my code to execute paste special


I have a small VBA code to copy a row from one sheet and paste to another, it works fine for paste but not for paste special, as I am trying to paste values only and not just paste.

this is my code, very basic. Noted that the pastespecial is changed to paste the code works fine.

thanks for you help

Private Sub CommandButton1_Click()

    a = Worksheets("Inventory List Costing Review").Cells(Rows.Count, 1).End(xlUp).Row

    For i = 10 To a

        If Worksheets("Inventory List Costing Review").Cells(i, 19).Value = "Completed" Then

            Worksheets("Inventory List Costing Review").Rows(i).Copy
            Worksheets("Completed by Sales").Activate
            b = Worksheets("Completed by Sales").Cells(Rows.Count, 1).End(xlUp).Row
            Worksheets("Completed by Sales").Cells(b + 1, 1).Select
            ActiveSheet.PasteSpecial Paste:=xlPasteValues, operation:=xlNone
            Worksheets("Inventory List Costing Review").Activate

        End If

    Next

    Application.CutCopyMode = False

    ThisWorkbook.Worksheets("Inventory List Costing Review").Cells(1, 1).Select

End Sub

Solution

  • The following should work (cleaned up a bit of clutter along the way). Although if you're just copying data from cells it would be faster to assign the values directly to the destination cells instead of copy-pasting.

    Private Sub CommandButton1_Click()
    
    With Worksheets("Inventory List Costing Review")
        a = .Cells(Rows.Count, 1).End(xlUp).Row
        
        For i = 10 To a
            If .Cells(i, 19).Value = "Completed" Then
            
                .Rows(i).Copy
                b = Worksheets("Completed by Sales").Cells(Rows.Count, 1).End(xlUp).Row
                Worksheets("Completed by Sales").Cells(b + 1, 1).PasteSpecial Paste:=xlPasteValues, operation:=xlNone
            End If
        Next
    
        Application.CutCopyMode = False
        .Cells(1, 1).Select
    End With
    
    End Sub