+ Reply to Thread
Results 1 to 7 of 7

How do I make ThisWorkbook.Name a global variable?

  1. #1
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    How do I make ThisWorkbook.Name a global variable?

    Please Login or Register  to view this content.
    I am trying to make a global variable that takes the value of ThisWorkbook.Name when opened. This way I can change the excel files name but use a variable in my code when switching between 2 workbooks. Can someone show me the syntax to do this? In the example code I declare a public variable and then attempt to rename it when opening the workbook. However, in WorkSheetExists the "subscript is out of range."
    Last edited by 111StepsAhead; 11-22-2011 at 11:35 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: How do I make ThisWorkbook.Name a global variable?

    Hi,

    In your code there's no need for the "", it won't recognise it as a variable with it:

    Please Login or Register  to view this content.

    You could have the workbook as an option argument in the function:


    Please Login or Register  to view this content.

    You can then call it like this specifying the workbook or just leave it out to operate on the active workbook:

    Please Login or Register  to view this content.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: How do I make ThisWorkbook.Name a global variable?

    The problem is the end user might change the name of the workbook with this code in it. Since I want to activate this workbook I can't be sure what the name of the workbook might be. Then the user will have to get into VBA themselves to change the workbook name.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How do I make ThisWorkbook.Name a global variable?

    workbooks(2) won't change by renaming a workbook.

    There's no need to 'activate' or 'select' in VBA.

    you can add a sheet using:

    Please Login or Register  to view this content.



  5. #5
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: How do I make ThisWorkbook.Name a global variable?

    snb,

    How do you know the index?

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How do I make ThisWorkbook.Name a global variable?

    workbooks(1): personal.xls
    workbooks(2): secondly opened workbook

    to verify:

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: How do I make ThisWorkbook.Name a global variable?

    I won't know when the user opens the workbook however. It could be the 2nd workbook opened or the 5th.

    So I guess I'll just confirm which one it is when they open it and go from there. Thanks for your help.

+ 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