+ Reply to Thread
Results 1 to 6 of 6

Assigning variable to open workbook

  1. #1
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Thumbs up Assigning variable to open workbook

    Hello

    I want to create a report that pulls in data from a shared workbook containing multiple worksheets.
    Unfortunately I'm stumbling fairly early as I can't set the variable to refer back to the original file containing the data once it has been opened. Basically I need to be able to toggle between two workbooks but the name of one will vary every time it is used to create the report. Code is as follows:


    Please Login or Register  to view this content.

    Works OK to this point but doesn't go back to file opened at step 1 above but stays in current as this is now deemed to be the active workbook


    Thanks in anticipation
    Last edited by DavidBW; 07-21-2011 at 08:28 AM. Reason: code tags

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Assigning variable to open workbook

    DavidBW,

    In the future, please wrap your code in code tags. As to your question, you need to set a variable before opening the workbook, and then set the new workbook to its own variable. Here's an example:

    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Re: Assigning variable to open workbook

    thanks, that bit works but now I get a "Run-time error 13 - Type mismatch" error when trying to activate the wb1 or wb2 workbooks.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: Assigning variable to open workbook

    You don't use:
    Please Login or Register  to view this content.
    for example as THISWB is a workbook object, not the name of a workbook. It's just:
    Please Login or Register  to view this content.
    It is however almost never necessary to actually activate or select anything in code.
    Remember what the dormouse said
    Feed your head

  5. #5
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Re: Assigning variable to open workbook

    sorry, just to clarify what I would like to do in this macro and that is to simply toggle between wb1 and wb2 (I will be copying data from wb2 into wb1)

    thanks

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: Assigning variable to open workbook

    Based on your posted sample:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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