简体   繁体   中英

xlPastevalues is giving me an object defined error VBA

Sub foo()

Dim ws As Worksheet: Set ws = Sheets("inbd")
Dim wsDestination As Worksheet: Set wsDestination = Sheets("test")

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    ws.Range("A1:N" & LastRow).AutoFilter Field:=1, 
Criteria1:=Worksheets("test").Cells(1, 26).Value
    ws.Range("f2:f" & LastRow).SpecialCells(xlCellTypeVisible).Copy Range("C6")
DestinationRow = wsDestination.Cells(wsDestination.Rows.Count, "C").End(xlUp).Row + 1
    wsDestination.Range("C" & DestinationRow).PasteSpecial xlPasteValues

Application.CutCopyMode = False
ws.Range("A1:N" & LastRow).AutoFilter Field:=1




End Sub

Good evening, Paste values part is giving me an object defined error and I don't know why

You want a test to ensure there are visible cells to copy from:

If Application.WorksheetFunction.Countif(ws.Range("A1:N" & LastRow),Worksheets("test").Cells(1, 26).Value) > 0

. A test that you have more than one cell for your filter

If LastRow > 1 

and you also have already pasted with this line:

ws.Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).Copy Range("C6")

You pasted to C6 and now have an empty clipboard so cannot paste again.

Perhaps you wanted:

ws.Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).Copy 
DestinationRow = wsDestination.Cells(wsDestination.Rows.Count, "C").End(xlUp).Row + 1
wsDestination.Range("C" & DestinationRow).PasteSpecial xlPasteValues

You have already pasted the copied cells into the inbd sheet C6.

Your code does not make much sense. You determine the last row of data before you filter, you copy and paste, then you paste again. I suggest you first filter, then determine the last row in both sheets, then do the copy and paste special right after one another without the wrong paste to C6 step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM