+ Reply to Thread
Results 1 to 11 of 11

Refer to Range in Another Workbook

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Refer to Range in Another Workbook

    I try to keep my macro code as simple and readable as possible.
    I know that I can refer to a named range (with global scope) within ThisWorkbook by simply using the range name in brackets, such as:
    Please Login or Register  to view this content.
    But how can I most simply refer to a (global) range name in another (open) workbook? The other workbook will be open, and I will have assigned a variable name to it. For example, suppose my macro opens a second file named Budget2018.xlsx, which contains a sheet with the codename MainSheet, and which contains a named range (on MainSheet, with global scope) called TotalIncome (which also does NOT match any named range in ThisWorkbook). Can I do this:
    Please Login or Register  to view this content.
    or is some other syntax required?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Refer to Range in Another Workbook

    I think you have to qualify the sheet because its in an external workbook.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Refer to Range in Another Workbook

    So if that sheet is already codenamed MainSheet (via the sheet Properties, not just the name displayed on the sheet tab), I could use:
    Please Login or Register  to view this content.
    correct?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Refer to Range in Another Workbook

    I don't think code names are allowed to reference sheets in an external workbook. You could use the sheet tab name (as I use in my previous reply) or the sheet index number. Alternatively, set a worksheet variable when you open the BudgetFile.

  5. #5
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Refer to Range in Another Workbook

    I avoid using sheet tab names or sheet numbers, since users can change those (and then my macros won't work properly).
    I like your idea of using a worksheet variable. Could I do that (using the sheet codename) right after opening the file (when it's still the active workbook):
    Please Login or Register  to view this content.
    or would I still need to use the tab name or sheet number?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Refer to Range in Another Workbook

    Quote Originally Posted by Merf View Post
    I avoid using sheet tab names or sheet numbers, since users can change those (and then my macros won't work properly).
    I like your idea of using a worksheet variable. Could I do that (using the sheet codename) right after opening the file (when it's still the active workbook):
    Please Login or Register  to view this content.
    or would I still need to use the tab name or sheet number?
    You still couldn't directly use a code name for an external workbook sheet regardless if it's the active workbook.

    Here's a kludge around it using the sheet code name.

    Please Login or Register  to view this content.
    Alternatively, you could loop through all the sheets in BudgetFile and check for a specific value in a cell to determine which one is the MainSheet.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Range in Another Workbook

    You can use a codename in another workbook directly, but only by setting a reference to that workbook's project.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Refer to Range in Another Workbook

    Quote Originally Posted by xlnitwit View Post
    You can use a codename in another workbook directly, but only by setting a reference to that workbook's project.
    That's what I suggested, but I don't consider that being Directly.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Range in Another Workbook

    Where did you suggest that? I can't see that anywhere.

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Refer to Range in Another Workbook

    Please Login or Register  to view this content.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Range in Another Workbook

    Ah, that's not what I mean. I mean set a reference using Tools- References (you have to change the project name from the default VBAProject) and you can then directly refer to the sheet using projectname.codename.

+ 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: 7
    Last Post: 08-08-2014, 11:01 AM
  2. Refer to a Workbook level named range
    By ptownbro985 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2014, 06:02 PM
  3. [SOLVED] Cannot refer to a dynamic range in a closed workbook
    By BNCOXUK in forum Excel General
    Replies: 7
    Last Post: 01-07-2014, 09:56 AM
  4. COMBO BOX-Refer to a range on different workbook
    By CESAR V. ARROYO in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-29-2010, 05:00 PM
  5. VBA Refer to named range in different workbook
    By globalpontoon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2010, 03:20 AM
  6. named range ?::how to I refer to current workbook without using its name?
    By Rajni R in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 07:05 AM
  7. refer to dynamic range in closed workbook with ADO
    By MattShoreson in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2005, 06:26 AM

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