+ Reply to Thread
Results 1 to 3 of 3

Writing ActiveWorkbook Name to a variable

Hybrid View

  1. #1
    Registered User
    Join Date
    11-17-2009
    Location
    Exeter,England
    MS-Off Ver
    Excel 2007
    Posts
    6

    Writing ActiveWorkbook Name to a variable

    Hi

    I am struggling with a macro that copies data from one workbook (with a standard name) to another - with a name which can vary.

    To explain, I run the macro on a currently open workbook (name could be anything) and open another workbook (name known) and step through the sheets copying various blocks of data across to the first workbook. I need to Activate the 2 workbooks alternatively as I move data back and forth and move from sheet to sheet.

    What I can't work out is how to determine in code what the "unknown" workbook name is so that I can make sure I activate the right workbook in the code.

    I tried Activating the workbook with the "unknown" name and setting a variable = ActiveWorkbook.Name. However the value of this variable changes during the code run when you activate the other book. Is there any way to collect this value and "freeze" it so it won't change as the code runs?

    At the moment I have got round it by ensuring the "unknown" book is the only one open before I start and refer to it as Workbooks(1) in the code.

    Any bright ideas would be welcome.

    Thanks
    Last edited by toveyj; 11-24-2009 at 12:10 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Writing ActiveWorkbook Name to a variable

    Hi,

    This is a common requirement. The techique I generally use is to declare a couple of variables:

    Dim wbMyBook As Workbook, wbTemp as Workbook
    Make the first line of your macro

    Set wbMyBook = ActiveWorkbook
    then as soon as you've opened the second workbook add the line

    Set wbTemp=ActiveWorkbook
    Now you can easily refer in code to whichever workbook you want.

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    11-17-2009
    Location
    Exeter,England
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Writing ActiveWorkbook Name to a variable

    Hi Richard

    Thanks very much - I thought there must be an easy answer but I couldn't work out what it was. I have given it a go and it seems to work. Again - many thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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