+ Reply to Thread
Results 1 to 9 of 9

Capturing open workbook as variable?

  1. #1
    Registered User
    Join Date
    09-23-2013
    Location
    Gold Coast, Australia
    MS-Off Ver
    2010
    Posts
    50

    Capturing open workbook as variable?

    I've discovered that the code I have for pulling a template worksheet into an open workbook runs into problems that results in the code ThisWorkbook referencing the template, and not the open workbook.

    Please Login or Register  to view this content.
    I'm trying to capture the open workbook name into a variable to try and get my macro to work properly. The target (open workbook) will have multiple locations and names, but the template worksheet will be stored in a static location.


    Please Login or Register  to view this content.
    Any help would be greatly appreciated!
    Last edited by aaron.irvine; 10-10-2013 at 07:35 PM.

  2. #2
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: Capturing open workbook as variable?

    ThisWorkbook should refer to the workbook in which the macro is running.

    You can declare a workbook variable:
    Please Login or Register  to view this content.
    and then set that variable to a workbook using "Workbook.Open":
    Please Login or Register  to view this content.
    or you can select an already open workbook using its name

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-23-2013
    Location
    Gold Coast, Australia
    MS-Off Ver
    2010
    Posts
    50

    Re: Capturing open workbook as variable?

    Thanks a lot gyclone!

    Is there the ability to set wb to equal the workbook the operator activates the macro button in?

    My concern is that the workbook names will change, and I need it to work autonomously without coding changes if possible.

    The biggest problem I see that I'm having is that the macro references the code in the source workbook after it's been imported into the target workbook. If I could get the macro to point to the local code (for want of the proper terminology) instead of pointing back to the source workbook, I think I would avoid having to create a variable.

  4. #4
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: Capturing open workbook as variable?

    I'm afraid I'm not sure of the chronology of target vs source vs open. Nor am I sure of whether all the code exists in one workbook or whether you're trying to use one workbook to fire off code in another workbook. "ThisWorkbook", if used in the workbook with the macro button, should capture that workbook. You can use more than one workbook variable and assign each one as each additional workbook opens, as I showed above; just use different names:
    Please Login or Register  to view this content.
    Once each variable has been assigned, you can use them throughout the procedure.

    If you are using a macro in one workbook to launch a macro in another workbook, you can pass the necessary workbook names between the procedures, in the same manners you could if all the code was in the same workbook.

  5. #5
    Registered User
    Join Date
    09-23-2013
    Location
    Gold Coast, Australia
    MS-Off Ver
    2010
    Posts
    50

    Re: Capturing open workbook as variable?

    Sorry for not being clearer with the relationships of the files gyclone!

    target and open are the same file. This workbook is used by employees to capture data.

    Once the worksheet is full, they need to import a new worksheet template from the source workbook that is stored in another location.

    Ideally the macro code needs only exist in the target & open workbook - but each of the imported worksheets need to have an active macro button that will allow the employees to once again import another template worksheet from the source workbook.

    Everything works fine for the first import, but when I try to import a new worksheet from an imported worksheet it does not work. I believe my issue lies in where I am embedding the code, and how I am referencing it through the macros. I've tried embedding the code in the sheets, and in the workbook & neither worked for me.

    What I need is for the macro on an imported sheet to reference the code in the target & open workbook - and not reference back to the source workbook.

    I hope this makes it clearer? Thank you!

  6. #6
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: Capturing open workbook as variable?

    Okay, I think I'm following, more or less.

    Scenario: You have two workbooks; one in which a user is entering data (Workbook1) and one which holds the data-entry template (Workbook2). When user clicks button on data-entry page in Workbook1, a template should be imported from Workbook2 into Workbook1. The newly-added template sheet added to Workbook1 needs a button which, when pressed, will fire off the same template-import macro as before, importing yet another copy of the template worksheet from Workbook2 into Workbook1.

    Assuming the above scenario is correct, I would create the new buttons on the new sheets at the time they are being imported. This should simplify things since you'll only need to reference the template workbook long enough to copy the sheet over. Once the sheet is copied over, you can close the template workbook, add the new button, and assign it to the same macro you used originally.

    I thought it would be clearer if I demonstrated with a fresh and very simple example. The code below covers the whole scenario (see attached workbooks for working example).
    Please Login or Register  to view this content.

    Let me know if I've completely misunderstood or if you need more help plugging it into your situation.
    Attached Files Attached Files
    Last edited by gyclone; 10-10-2013 at 10:24 PM. Reason: Removed Redundant code

  7. #7
    Forum Contributor
    Join Date
    02-15-2010
    Location
    Everett, WA
    MS-Off Ver
    All versions; most components
    Posts
    188

    Re: Capturing open workbook as variable?

    Sorry, it's been a long day; you should make the following change:

    Please Login or Register  to view this content.
    should be re-ordered, so we set the wsDestination variable right after we import the template sheet, just to be safe:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-23-2013
    Location
    Gold Coast, Australia
    MS-Off Ver
    2010
    Posts
    50

    Re: Capturing open workbook as variable?

    Please don't apologise mate! You've gone above and beyond to help me out here, thank you so much!

    I'm just tweaking the code now, I'll report back a little later to let you know how it goes. You hit the nail right on the head though for how everything is structured. I see my biggest mistake was embedding the code into the sheets and not into the button!

  9. #9
    Registered User
    Join Date
    02-23-2012
    Location
    Almaty, Kazakhstan
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Capturing open workbook as variable?

    It does not matter. You are welcome.

+ 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. 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
  2. [SOLVED] Open Workbook, hidden, get cell data into a variable
    By Bruce Tjosvold in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2012, 04:27 PM
  3. Open workbook as variable
    By Karen311261 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-29-2012, 06:56 AM
  4. Assigning variable to open workbook
    By DavidBW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-21-2011, 06:04 AM
  5. Open Workbook using a variable
    By AlanAnderson in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-29-2010, 07:51 AM

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