+ Reply to Thread
Results 1 to 14 of 14

Referencing named range in VBA

  1. #1
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Referencing named range in VBA

    Say I have a variable ("noDates") in worksheet (Workbooks("Book1").Sheets("sheet1")) which calculates to an integer. I want to use this value in my macro to perform other operations.

    Can't figure out all the Dim, set, and all other statements to make it happen. It should be very simple, but I'm stuck.

    All help will be greatly appreciated.
    Last edited by 6StringJazzer; 10-04-2021 at 04:34 PM. Reason: do not bold entire post; better titles please

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Referencing named range in VBA

    I changed your title from "Simple reference question" because it does not describe your question. Normally I would cite you for a rule violation but I found it easier to just change it and answer your question. But please take the time to review our rules. There aren't many, and they are all important. Rule #1 is Use Good Titles.

    By "variable" do you mean "named range"? If so then your code can refer to

    Sheets("sheet1").Range("noDates")

    If you tell us more about your code I can give you more specific direction.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Re: Referencing named range in VBA

    Thanks, and apologies for the wrong title. Will have to do better, it's one of the problems of being a novice.

    Now "nodates" is not a named range, rather it's an integer which is calculated from sheet contents. It is created via the Name Manager in the worksheet, as a calculation.

    Also, can the value of "noDates" be given a name within the macro itself?

    As always, I really appreciate your help.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Referencing named range in VBA

    How is noDates defined in Name Manager? If noDates was created in Name Manager, it is either a named range or named formula, and my answer is still valid.

    If you want you can do something like this:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Re: Referencing named range in VBA

    I tested it and got:

    Run-time error '1004':
    Application-defined or object-defined error

    noDates is the count of a range of cells in Sheet1. So, it's a named formula.

    Thanks for your patience.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Referencing named range in VBA

    Please attach a file illustrating the problem. See yellow banner at the top of the page.

    The paper clip icon does not work for attachments. To attach a file:

    1. Under the text box where you type your reply click the Go Advanced button.
    2. On the next screen scroll down and under the Attachments section click the Manage Attachments link, which will show a pop-up window.
    3. Click the Choose File button to select a file to attached.
    4. Click the Upload button to upload the file.
    5. Then click the Close This Window button. Your file is now attached to your post.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Referencing named range in VBA

    Try:

    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  8. #8
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Re: Referencing named range in VBA

    I'm not sure it worked by I uploaded it. The sheet in question is "Track Change".
    Attached Files Attached Files

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Referencing named range in VBA

    There is no code in this book so I don't know how you want to use it. Just use

    [nodates]

  10. #10
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Re: Referencing named range in VBA

    Amazingly that simple solution works. Many thanks again for helping me.

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Referencing named range in VBA

    Actually I have no idea why this is the only thing that works. I tried several other things that should work but this was the one that did.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Referencing named range in VBA

    It's a named formula that returns a simple data type, so the only way to access its value is essentially by evaluating its formula (which is what the [] notation does.

  13. #13
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Re: Referencing named range in VBA

    Once again, thanks for your patience and persistence.

  14. #14
    Forum Contributor
    Join Date
    09-12-2021
    Location
    Mexico
    MS-Off Ver
    Office 2019
    Posts
    169

    Re: Referencing named range in VBA

    That makes it clear to me for future reference. Thanks for posting.

+ 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. [SOLVED] Simple "lock the cell reference" question
    By LewisBosworth in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-02-2019, 10:58 PM
  2. Simple Cell Reference question
    By sjak in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-01-2008, 10:58 AM
  3. VBA value reference from a cell ..simple question
    By Richard Flame in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2007, 07:21 PM
  4. Simple? Reference Question
    By j in forum Excel General
    Replies: 1
    Last Post: 12-02-2005, 10:40 AM
  5. Simple Simple Excel usage question
    By BookerW in forum Excel General
    Replies: 1
    Last Post: 06-23-2005, 05:05 PM
  6. Cell Reference... simple question
    By bondcrash in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-08-2005, 05:53 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