+ Reply to Thread
Results 1 to 5 of 5

Trouble setting workbook variable

  1. #1
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Trouble setting workbook variable

    Hi,

    I have two workbooks, one a main Tracker and the other a template that is populated with data then saved. I'm writing the code to reside in the main tracker but reference and manipulate the template as required.
    In order to keep the code tidy and more readable I'm trying to declare a couple of workbook variables and then worksheets variables to act as shorthand when specifying ranges across the two workbooks.

    I can set the variable for the tracker as this uses the Thisworkbook property but I'm getting a 'subscript out of range' error on the template one:

    Please Login or Register  to view this content.
    I'm guessing I somehow have the syntax wrong but can't quite work it out. I've also run the code with just the Tracker open and with both the Tracker and the Template open, but it makes no difference.

    Ideally, I'd like to get the workbook name from a full filepath held in a range using a function I have to get the file name from a file path (this on it's own works fine, but same error when combined with the workbook variable) but I figure one step at a time!

    As always, any help appreciated.

    Thanks, TC
    Last edited by TC1980; 08-29-2013 at 11:52 AM. Reason: Code tags didn't work

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Trouble setting workbook variable

    Is PT Template open? Also you need to include the file extension, such as
    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Trouble setting workbook variable

    Thanks Solus.

    I thought I was including .xls (in the getfilename function) but I must have been a numpty as it works fine now. Also good to know that the file has to be open in order to set the variable.

    Thanks again, TC

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Trouble setting workbook variable

    You can set it without being open by opening it if you have the fullname (name & path):
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Trouble setting workbook variable

    Thanks - I did not know that.

    I will keep to setting the variable once open as I need to do some checks first to make sure the file exists.

    Thanks again, TC

+ 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. Replies: 3
    Last Post: 08-07-2013, 10:16 AM
  2. setting a workbook object using a workbook name that is a variable
    By amazingg64 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-07-2012, 08:23 PM
  3. Trouble Setting a Range
    By CJ944 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-12-2012, 10:53 AM
  4. Trouble Setting a cell to a value
    By jubi23 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2007, 02:52 PM
  5. Setting a workbook name as a variable...
    By apeface76 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-27-2005, 03:59 PM

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