+ Reply to Thread
Results 1 to 8 of 8

Can't do a simple copy paste due to application/object error

  1. #1
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    Can't do a simple copy paste due to application/object error

    Debugger pointing to code in the red when trying to copy from one workbook to another.


    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    01-07-2022
    Location
    Europe
    MS-Off Ver
    Office 365
    Posts
    473

    Re: Can't do a simple copy paste due to application/object error

    try this

    Please Login or Register  to view this content.
    <<< If you have valued anyone's contributions in this thread, please click * to thank them for their efforts

  3. #3
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    Re: Can't do a simple copy paste due to application/object error

    Quote Originally Posted by AskMeAboutExcel View Post
    try this

    Please Login or Register  to view this content.
    It worked. Would you mind explaining the logic behind why mine doesn't work?

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Can't do a simple copy paste due to application/object error

    Your code is in a worksheet code module, so any use of Range or Cells that is not qualified with a worksheet will refer to the sheet containing the code, not the active sheet. That means that in this line:

    Please Login or Register  to view this content.
    the Range property refers to the active sheet, and the Cells properties refer to a different sheet (the one with the Activate event code), which doesn't make sense.
    Rory

  5. #5
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    Re: Can't do a simple copy paste due to application/object error

    Quote Originally Posted by rorya View Post
    Your code is in a worksheet code module, so any use of Range or Cells that is not qualified with a worksheet will refer to the sheet containing the code, not the active sheet. That means that in this line:

    Please Login or Register  to view this content.
    the Range property refers to the active sheet, and the Cells properties refer to a different sheet (the one with the Activate event code), which doesn't make sense.

    Sorry, I thought Range(cells(2,1), cells(2,2)) = Range(B2 : C2)?

    I learnt its just a different way of expressing range, and it has worked fine before.

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Can't do a simple copy paste due to application/object error

    Not quite. Let’s say the code is in sheet1. Your original version:

    Please Login or Register  to view this content.
    is actually equivalent to:

    Please Login or Register  to view this content.
    As far as VBA is concerned, that means you want a reference to a range on Rawdata.ActiveSheet that starts at A5 on sheet1 and goes to another cell on sheet1, which doesn’t make sense. Note: you are not referring to the addresses of the cells (eg “B1”), you are using references to specific actual cells which exist on a specific sheet in a specific workbook.

  7. #7
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126
    So it seems I need to qualify cell reference since we are dealing with worksheet event. I understand it wouldn't be able to decipher otherwise which sheet cell reference is referring to.

    For workbook I should be able to skip it without necessitating workbook reference.

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Can't do a simple copy paste due to application/object error

    In a procedure in any worksheet code module (doesn't have to be an event procedure) any unqualified reference to Range or Cells will refer to a range on that sheet, whether it is active or not - it is equivalent to Me.Range or Me.Cells.

    In any other module (unless it happens to be a class that implements a Range property), it is the equivalent of Application.Range or Application.Cells, which will generally refer to the active sheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Application/Object defined error on copy/paste
    By radddogg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-09-2020, 08:05 AM
  2. Application/Error on Copy/Paste When Switching Between Workbooks
    By ShitLeopard in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-06-2015, 10:28 PM
  3. [SOLVED] Error '1004': Application-defined or object-defined error when trying to copy a range
    By TLeng in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-11-2014, 01:35 PM
  4. Need help with copy and paste formulas and application-defined error
    By mpjennings25 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-03-2013, 11:20 AM
  5. application or object defined error raised by some simple construct
    By aretai in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-21-2010, 02:27 AM
  6. [SOLVED] Application-Defined or Object-Defined Error on simple code
    By Fid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2005, 04:05 PM
  7. [SOLVED] Application-Defined or Object-Defined Error on simple code
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-22-2005, 05:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1