Compile error - Excel VBA

Anonymous
2020-04-14T14:34:25+00:00

Hello all,

Microsoft Excel version: Microsoft Professional + 2016

VBA Sub code: Clear_Existing_Data_Before_Paste ( )

I used below code a few times without any problems. This time I got a "Compile error: Invalid use of property" notification.

I`ve not made any changes to the coding, does somebody know how to write the code that it becomes more stable and less error-prone?

Below are two screenshots, first screenshot is of the notification and the second screenshot marked the error. 

Screenshot 1:

Screenshot 2:

Any idea is welcome, thanks for your effort.

Kind Regards,

Erik

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} vote

7 answers

Sort by: Most helpful
  1. Andreas Killer 144K Reputation points Volunteer Moderator
    2020-04-14T14:55:32+00:00

    If you pass an argument to the Copy method, the argument must be a Range object.

    https://docs.microsoft.com/en-ie/office/vba/api/excel.range.copy

    You try to pass the Delete method as argument, hence the error.

    I can only guess: Move the line wsN.Delete below wsDest.Range("A1")

    Andreas.

    0 comments No comments
  2. Anonymous
    2020-04-14T15:47:01+00:00

    Hello Erik

    I am V. Arya, Independent Advisor, to work with you on this issue. Was it working earlier with WsN.Delete in that place?

    I tried to replicate the error. I didn't get that.

    0 comments No comments
  3. Anonymous
    2020-04-14T15:53:09+00:00

    Hello Andreas, thanks for your reply. I made the change as you advised, unfortunately the code does not copy the data in the worksheet anymore. Sadly it does not work. Still, thanks for trying to find a solution. 

    Have a nice day!

    Kind Regard

    Erik

    0 comments No comments
  4. Anonymous
    2020-04-14T15:58:04+00:00

    Hello V. Arya, thanks for reaching out to me.

    It was working before, I run the code this afternoon and all of the sudden I got a Compile Error.

    I changed the formula as @Andreas proposed, the Compile error is gone, but the formula does not work paste the new data in the worksheet anymore. Do you have a other alternative for me, a code that I can enter that copy data from workbook (1), removes existing data in workbook (2) and paste new data in workbook (2)?  

    Please let me know if you want me to forward anymore details.

    Thanks for your help.

    BR

    Erik

    0 comments No comments
  5. Anonymous
    2020-04-14T16:06:08+00:00

    Try using following in place of WsDest.Range("A1")

    WsDest.Range("A1").PasteSpecial xlAll

    Your sequence has to be

    'First clear the destination sheet

    WsDest,Cells.Clear

    Then put your copy line followed by paste line

    WsDest.Range("A1").PasteSpecial xlAll

    Then you can use wsN.Delete

    Nothing should be between copy and paste statement

    0 comments No comments