+ Reply to Thread
Results 1 to 13 of 13

Referring to Named Ranges defined with OFFSET()

  1. #1
    Registered User
    Join Date
    12-13-2013
    Location
    Espoo, Finland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Referring to Named Ranges defined with OFFSET()

    Hi, I am fairly inexperienced in VBA and I'm having trouble with getting to the values in a named range in a subroutine.

    I have many named ranges in my project, some of which are defined as simple ranges ($A$1:$A$10 for example), and some of which are dynamically defined using OFFSET (for tables which change in length etc.)

    For a named range defined as a simple range, I can do the following:

    Please Login or Register  to view this content.
    Or something like that, anyway, I'm picking bits out of my code to show you. Now, my problem is that if I try to do this with a named range that has been defined using OFFSET, this method doesn't work. I have tried several different ways and I always get some error Can anyone show me how to use such named ranges within VBA?

    I am assuming that it's because .RefersToRange requires the Named Range to point to a range directly, but the ones with OFFSET point to a formula... I have tried removing the .RefersToRange completely, and tried it with .RefersTo and neither worked. Is there a way I can, instead of using the OFFSET named range, simply create a reference to the same range using the OFFSET formula within VBA? If so, how do I then get the values out within the For/Next loop?

    Thanks for any help, I can provide more info/code if required.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Referring to Named Ranges defined with OFFSET()

    dim r as excel.range
    dim array() as integer

    set r = thisworkbook.range("NAMED RANGE")

    redim array(r.cells.count)

    for each c in r.cells
    array(counter)=c.value
    next c


    havent tested
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Referring to Named Ranges defined with OFFSET()

    can you rebuild the range?
    Please Login or Register  to view this content.

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Referring to Named Ranges defined with OFFSET()

    The code you have will work with a valid named range of any type.

  5. #5
    Registered User
    Join Date
    12-13-2013
    Location
    Espoo, Finland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Referring to Named Ranges defined with OFFSET()

    Quote Originally Posted by Leon V (AW) View Post
    can you rebuild the range?
    Please Login or Register  to view this content.
    That worked great, thanks!

  6. #6
    Registered User
    Join Date
    12-13-2013
    Location
    Espoo, Finland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Referring to Named Ranges defined with OFFSET()

    Quote Originally Posted by nathansav View Post
    dim r as excel.range
    dim array() as integer

    set r = thisworkbook.range("NAMED RANGE")

    redim array(r.cells.count)

    for each c in r.cells
    array(counter)=c.value
    next c


    havent tested
    This also seems to work fine (if I removed the "thisworkbook."), there seem to be a couple of ways around this problem!
    Last edited by jaskamakkara; 12-19-2013 at 08:30 AM.

  7. #7
    Registered User
    Join Date
    12-13-2013
    Location
    Espoo, Finland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Referring to Named Ranges defined with OFFSET()

    Quote Originally Posted by Izandol View Post
    The code you have will work with a valid named range of any type.
    I have tried it several times and it doesn't, I'm afraid. It's annoying as it works fine for ranges defined simply as a range (rather than an offset formula)

  8. #8
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Referring to Named Ranges defined with OFFSET()

    May you post an example that does not work?

  9. #9
    Registered User
    Join Date
    12-13-2013
    Location
    Espoo, Finland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Referring to Named Ranges defined with OFFSET()

    May you post an example that does not work?
    Well, if you set up a named range in a sheet and define it by an offset formula, then try to pass the line:

    Please Login or Register  to view this content.
    Then it throws an error, unless I've got confused :S

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Referring to Named Ranges defined with OFFSET()

    If it raised an error for me I would not ask for an example.

  11. #11
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Referring to Named Ranges defined with OFFSET()

    is it a negative offset excel can throw a wobbler trying to go backwards in some areas

  12. #12
    Registered User
    Join Date
    12-13-2013
    Location
    Espoo, Finland
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Referring to Named Ranges defined with OFFSET()

    Quote Originally Posted by Izandol View Post
    If it raised an error for me I would not ask for an example.
    Well that was my example I can't explain why it doesn't work.... I noticed that in the Named Ranges manager, it always says that the "value" of my named ranges that I define using OFFSET is = {...} , this may have something to do with it? It doesn't seem to mean that the range is undefined, as I can still refer to it and use it for data validation, and pull data from it using the other methods suggested above.

  13. #13
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Referring to Named Ranges defined with OFFSET()

    Try
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ 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: 2
    Last Post: 06-03-2013, 08:26 AM
  2. Functions referring to Named Ranges
    By taa in forum Excel General
    Replies: 2
    Last Post: 03-25-2011, 04:17 PM
  3. [SOLVED] Referring to named ranges in a macro
    By Pontificateur in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2005, 02:05 PM
  4. Referring to named ranges
    By Hardy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-25-2005, 08:05 AM
  5. [SOLVED] Formulas referring to Name defined ranges.
    By PCLIVE in forum Excel General
    Replies: 3
    Last Post: 04-05-2005, 06:06 PM

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