+ Reply to Thread
Results 1 to 14 of 14

Using named ranges

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Using named ranges

    Looking to doa few things with a macro to process info. At the moment I am simply referring to the worksheets but I'd rather be able to refer to the named ranges. These are not sheet specific but acorss the whole book.

    My problem is just simply testing it I'm clearly getting something wrong. SO I have the following line of code:
    MsgBox ThisWorkbook.Names("ated_1").Cells(1, 1).Value
    Now in my head that should show me the contents of the top left cell of the named range "ated_1". AT the moment it just gives an error 1004 - application-defined ot object-defined error. I am sure to someone that knows about these things it is blindingly obvious but what am I getting wrong?

    And yes - ated_1 does exist

    TIA

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,767

    Re: Using named ranges

    Try
    MsgBox Range("ated_1").Cells(1, 1).Value

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Using named ranges

    You should access named range using Range().
    And should not be qualified with ThisWorkbook.

    MsgBox Range("ated_1").Cells(1, 1).Value
    Alternately taking advantage of Evaluate...

    MsgBox [ated_1].Cells(1, 1).Value
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  4. #4
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Using named ranges

    Thanks to both of you. I was under the impression that using Range would limit it to only the worksheet I was on. Clearly wrong. All sorted ta.

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Using named ranges

    You are welcome and thanks for the rep

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,767

    Re: Using named ranges

    Glad we could help & thanks for the feedback.

    I was under the impression that using Range would limit it to only the worksheet I was on
    It will if the named range is sheet scope, but not if it's workbook scope.

  7. #7
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Using named ranges

    Also tried columns.count but it, predicatably, gives me the number of columns, not the number of items across a row.

  8. #8
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Using named ranges

    I'm going to take advantage of your collective good nature now

    Having selected a range and performed most of what I want to do with it, the one thing I'm having a nightmare with is using COUNTA to see how many entries in a particular row of the range.

    I've tried all of the following (recreated so there may be typos)
    tbl_flag = WorksheetFunction.CountA(Range(ws_needed).Rows(info_loop))
    tbl_flag = WorksheetFunction.CountA(Range(ws_needed).cells(info_loop,2),cells(info_loop,20))
    tbl_flag = WorksheetFunction.CountA(Range(ws_needed).range(cells(info_loop,2),cells(info_loop,20)))
    For the first row it fails on there are two columns only but it will only return 1. What am I missing here?

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,767

    Re: Using named ranges

    This works for me
    MsgBox WorksheetFunction.CountA(Range("fluff").Rows(1))

  10. #10
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Using named ranges

    Quote Originally Posted by Fluff13 View Post
    This works for me
    MsgBox WorksheetFunction.CountA(Range("fluff").Rows(1))
    Thanks for that. I'll factor that back in

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Using named ranges

    What does "info_loop" and "ws_needed" hold?

    Also, typically speaking, you are better off using Application.CountA or Evaluate() rather than WorksheetFunction.

    Try following:
    Application.CountA(Range(ws_needed))
    Or
    Application.CountA(Range(cells(info_loop,2),cells(info_loop,20)))

  12. #12
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Using named ranges

    info_loop = number
    ws_needed = named range.

    DIdn't even know application.counta existed. Had always been told to use the worksheet function - every day's a school day!

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Using named ranges

    So ws_needed is range object? I assumed it was a string.

    If it is range object use it like...
    Application.CountA(ws_needed.Rows(1))
    I prefer Application.SomeFunction over WorksheetFunction, since when calculation ends up in error, it actually returns Error value rather than throwing code error.

    You can test using...
    Debug.Print Application.Match(1, Array("A", "B", "C"), 0)
    Debug.Print WorksheetFunction.Match(1, Array("A", "B", "C"), 0)
    List of Error constants returned via Application.SomeFunction below.
    Error_Val   Error Value
    #NULL!      Error 2000
    #DIV/0!     Error 2007
    #VALUE!     Error 2015
    #REF!       Error 2023
    #NAME?      Error 2029
    #NUM!       Error 2036
    #N/A        Error 2042
    Last edited by CK76; 05-25-2018 at 10:07 AM.

  14. #14
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: Using named ranges

    Handy. Thanks very much for that

+ 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. Copy values of all named range in wb1 to identically named ranges in wb2
    By JAMIAM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-24-2016, 06:58 PM
  2. [SOLVED] VBA creates named ranges, but named ranges disappear
    By BrotherNeptune in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2015, 03:22 PM
  3. Define only 2 named ranges from a list of named ranges...
    By abhi900 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-29-2014, 04:20 AM
  4. Looping Mutliple Named Resized Ranges in next empty row below another named range
    By gingumdog in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-19-2014, 08:15 PM
  5. [SOLVED] Determining if the value of a cell can be a named range, then assigning named ranges after
    By Romulo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-15-2013, 06:05 PM
  6. Copy data in named ranges to a newer version of the same template to identical ranges
    By handstand in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-21-2006, 10:51 AM
  7. [SOLVED] Named ranges-is there a an easy way to reference a single value in a named range?
    By pspkim@gmail.com in forum Excel General
    Replies: 1
    Last Post: 03-21-2006, 06:40 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