+ Reply to Thread
Results 1 to 12 of 12

VBA to define range without specifying sheet

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    VBA to define range without specifying sheet

    Hi all,

    Is there a way to define a range as my "TimePeriod", let's say $AL$5:$AL$1500 without specifying the sheet? I would need to use the exact same range on different sheets, so I want to specify which sheet it should refer to within the formula I'm using.

    e.g

    original formula:

    Please Login or Register  to view this content.
    adjusted formula with defined range:

    Please Login or Register  to view this content.
    my range is defined in the following VBA code which is linked to my form control box where one can select the time period:

    Please Login or Register  to view this content.
    As you can see, in the above code, the range is still defined by referring to a particular sheet (Location Plan) though, I want it to only define the range and then specify the sheet it should refer to in the formula I mentioned at the top.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Something like the above...

    Is that even feasible? (The above doesn't work...)

  2. #2
    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: VBA to define range without specifying sheet

    Hi,

    It's not possible with just a defined name. You can define a name to work in the sheet it's called from, but not in such a way that you can specify the worksheet when calling it, unless you use a UDF or INDIRECT.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Valued Forum Contributor
    Join Date
    05-11-2013
    Location
    Wales
    MS-Off Ver
    Excel 2010
    Posts
    586

    Re: VBA to define range without specifying sheet

    Hi esbencito,

    Is the data in the "TimePeriod" range different on each sheet?
    How many sheets are there?

    Regards

    peterrc

  4. #4
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to define range without specifying sheet

    Quote Originally Posted by peterrc View Post
    Hi esbencito,

    Is the data in the "TimePeriod" range different on each sheet?
    How many sheets are there?

    Regards

    peterrc
    The data is indeed different on each sheet and there are about 4 sheets...

  5. #5
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to define range without specifying sheet

    Quote Originally Posted by xlnitwit View Post
    Hi,

    It's not possible with just a defined name. You can define a name to work in the sheet it's called from, but not in such a way that you can specify the worksheet when calling it, unless you use a UDF or INDIRECT.
    How would a UDF or INDIRECT look like?

  6. #6
    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: VBA to define range without specifying sheet

    You might define the TimePeriod name as
    ="$AL$5:$AL$1500"
    and then use
    =INDIRECT("'Location Plan'!"&timeperiod)
    to refer to the range on a specific sheet. This will be a volatile function but that is largely unavoidable with your approach.

  7. #7
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to define range without specifying sheet

    Please Login or Register  to view this content.
    so the above code would be adjusted to the following?

    Please Login or Register  to view this content.
    And then my formula that refers to that range would look like below?

    Please Login or Register  to view this content.
    correct? or did I forget something? As it doesn't seem to work (yet). Did you try it with a similar example?

  8. #8
    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: VBA to define range without specifying sheet

    Not quite- your name needs to contain a string not a range, thus
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to define range without specifying sheet

    Please Login or Register  to view this content.
    I see! and how would I fixed the part of the code with the "Offset"? It's showing me a syntax error for these...

  10. #10
    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: VBA to define range without specifying sheet

    You'll need to use a range for the offset and then get its address
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to define range without specifying sheet

    That works incredibly well!! Thanks so much!

  12. #12
    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: VBA to define range without specifying sheet

    You're welcome. Thank you for the rep.

+ 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: 0
    Last Post: 09-17-2017, 06:30 PM
  2. Replies: 6
    Last Post: 01-17-2017, 11:44 AM
  3. [SOLVED] Define new range names based on existing range name offset 1 column to right
    By jprlimey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2014, 07:53 PM
  4. excel macro range define to another sheet / named range
    By koi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-21-2013, 10:25 AM
  5. Need Activate Sheet first in order to define range ?
    By cladelpino in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-03-2011, 01:27 PM
  6. Define named range where user can define size
    By nahousto in forum Excel General
    Replies: 4
    Last Post: 07-06-2009, 05:09 PM
  7. define range name, use on VBA selected sheet
    By Bob Swan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2005, 09:05 AM

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