+ Reply to Thread
Results 1 to 5 of 5

Copy Range from workbook with variable file name

  1. #1
    Forum Contributor
    Join Date
    06-07-2013
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    158

    Copy Range from workbook with variable file name

    Hi all

    Could someone please help?

    I'm looking to copy ranges from one workbook to another, however I don’t know how to reference the workbook with a variable file name and pass it to the other workbook.

    The code will start in workbook 1 which has the variable file name. It then calls a subroutine in workbook 2 with a static name.

    The code in workbook 2 then copies ranges from workbook 1. It has to work this way.

    Any help is greatly appreciated

    Thanks

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Copy Range from workbook with variable file name

    Use Application.run.

    I passed the workbook object as the input parameter. If you want to pass the workbook name pass Thisworkbook.Name or Thisworkbook.Fullname.

    In this example, it is assumed that the slave and master XLSM files are in the same folder and that both are open. Data is copied from sheet2 starting at A2 to the last cell with data in column A. It is pasted into sheet2 of the master file in the next empty cell after the last data cell in column A.

    In a Module in the slave file:
    Please Login or Register  to view this content.
    You can also use the code above in your Personal workbook if you don't want to add it to all of your slave workbooks. Pass Activeworkbook as the input parameter in that case where the slave workbook is open and active as-well-as the master workbook.

    In a Module named mSlaveMaster in the master file:
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    06-07-2013
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Copy Range from workbook with variable file name

    I have used this to identify the file name but not sure how to use this in workbook 2

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    06-07-2013
    Location
    Bournemouth
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Copy Range from workbook with variable file name

    We seemed to reply at the same time.

    I can see how your code works, however I will be working with variable "slave" files to read from.


    Quote Originally Posted by Kenneth Hobson View Post
    Use Application.run.

    I passed the workbook object as the input parameter. If you want to pass the workbook name pass Thisworkbook.Name or Thisworkbook.Fullname.

    In this example, it is assumed that the slave and master XLSM files are in the same folder and that both are open. Data is copied from sheet2 starting at A2 to the last cell with data in column A. It is pasted into sheet2 of the master file in the next empty cell after the last data cell in column A.

    In a Module in the slave file:
    Please Login or Register  to view this content.
    You can also use the code above in your Personal workbook if you don't want to add it to all of your slave workbooks. Pass Activeworkbook as the input parameter in that case where the slave workbook is open and active as-well-as the master workbook.

    In a Module named mSlaveMaster in the master file:
    Please Login or Register  to view this content.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Copy Range from workbook with variable file name

    You need to choose to use the string or the object to pass. You dimmed your variable as the workbook object but then assigned it the value as a string.

    So, if you are passing the workbook name, use ActiveWorkbook.Name to pass. IF you are passing the workbook object, then pass ActiveWorkbook.

    You could pass the function name if you fix the issue I first explained here.

    If your master workbook is always going to use the activeworkbook, you can just use that directly without passing it as a parameter with a value.

    Without see what you are doing with other code, I don't know what other advice to give.

+ 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. [SOLVED] copy common cells and variable range from multiple worksheets to single master workbook
    By tg7384 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-15-2014, 03:05 PM
  2. [SOLVED] Open Existing File and Copy Range to Current Workbook
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-16-2013, 01:37 PM
  3. Copy variable range from variable workbook to variable range on current workbook
    By Locster79 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2013, 10:37 AM
  4. How to copy values from one OPEN workbook (with variable file name) to another
    By Kitko in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-25-2013, 01:50 PM
  5. Copy named range from file list to summary workbook
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2006, 04:20 PM

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