+ Reply to Thread
Results 1 to 6 of 6

Copying from wb1 to wb2 throws subscript out of range error

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

    Question Copying from wb1 to wb2 throws subscript out of range error

    My idea is to use FilePathToWb variable inside the Workbooks(FilePathToWb) argument to copy from ActiveWorkbook (sourceWb) to opened workbook (targetWb); close the sourceWb at the end so that I am left with targetWb only.

    The debugger highlights the red font below.


    Please Login or Register  to view this content.
    Last edited by immigrated4urjob; 12-22-2021 at 11:55 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copying from wb1 to wb2 throws subscript out of range error

    Where is targetWb variable defined?

    targetWb as object

    set targetWb=

    subscript out of range error means the object( Worksheet) you are trying to access does not exist.
    Last edited by AB33; 12-22-2021 at 11:45 AM.

  3. #3
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,742

    Re: Copying from wb1 to wb2 throws subscript out of range error

    Just typed, without test:

    Please Login or Register  to view this content.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

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

    Re: Copying from wb1 to wb2 throws subscript out of range error

    Quote Originally Posted by KOKOSEK View Post
    Just typed, without test:

    Please Login or Register  to view this content.
    Weird bug. But I narrowed to one issue.

    Essentially without activating SourceWb window the copy method was failing. I don't understand the why though, I just know this is the reason as I thought providing workbook name would suffice the copy operation.

    So I needed to do before the copy line was add the following:

    Please Login or Register  to view this content.
    followed by rest of the code....

    Thanks for the help.

  5. #5
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2402-17328.20068
    Posts
    1,359

    Re: Copying from wb1 to wb2 throws subscript out of range error

    It is becuase your workbooks, worksheets and ranges are not defined properly, see below where i have defined everything. It should work without needing to activate anything.

    Please Login or Register  to view this content.

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

    Re: Copying from wb1 to wb2 throws subscript out of range error

    Quote Originally Posted by CheeseSandwich View Post
    It is becuase your workbooks, worksheets and ranges are not defined properly, see below where i have defined everything. It should work without needing to activate anything.

    Please Login or Register  to view this content.
    Thanks, it helps me think about coding in a more refined way. I think at the end when I defined set with full reference, it worked fine without needing to activate workbook.

+ 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. Copying Array Gives SubScript Out Of Range Error
    By Genus Max in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-03-2020, 05:29 PM
  2. error 9 subscript out of range when copying table to another sheet
    By MaartenRo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2020, 01:50 AM
  3. [SOLVED] error message 9: subscript out of range when copying table to another sheet
    By MaartenRo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-02-2020, 01:36 AM
  4. [SOLVED] Copying listrow throws an error (copy method of range class failed) why?
    By LIL2606 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-26-2019, 09:32 AM
  5. [SOLVED] Subscript out of range in VBA copying a template
    By SethFD in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-25-2017, 01:41 PM
  6. [SOLVED] Modifying Copy - Destination Range throws App Error '1004'
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-22-2016, 02:53 PM
  7. Copying Sheet - subscript out of range error
    By GSP@DCS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2006, 09:00 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