+ Reply to Thread
Results 1 to 11 of 11

How to use named ranges in VBA?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2024
    Location
    staines uk
    MS-Off Ver
    2000
    Posts
    3

    How to use named ranges in VBA?

    Suppose you have a table that looks like this:

    Fruit Apples Oranges etc

    John (numbers)

    Bill

    Sue

    etc



    and you insert > name > create the names in the top row & L column,
    then in a formula you can say things like

    = John Apples

    or

    = Apples + Oranges

    Meaning the values in that row. Please is there any way to do the equivalent
    in VBA? If (in VBA) you have option explicit and you say

    x = John( 2)

    you get an undefined variable error.

    Sorry if this is a FAQ. I searched. Only thing I could find that looks relevant
    was is 2006 . Somebody said " use the INDEX function" but I didnt
    say how to do this. INDEX requires numerical arguments, so it cannot use
    the defined names .

    Thank you.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,363

    Re: How to use named ranges in VBA?

    Fast answers need clear examples.

    See yellow banner at top of the page on how to attach a workbook.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    422

    Re: How to use named ranges in VBA?

    If you have a range named 'John', you can refer to in in VBA as
    Range("John")

  4. #4
    Registered User
    Join Date
    03-24-2024
    Location
    staines uk
    MS-Off Ver
    2000
    Posts
    3

    Re: How to use named ranges in VBA?

    thank you for replies. I tried to attach a file.
    In it I created the names in row 1 & col A & in col G are some formulae that
    refer to cells in these ranges.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    422

    Re: How to use named ranges in VBA?

    This will give you the number of apples for John:
    Intersect(Range("john"), Range("apple")).Value
    and this will give apples and pears
    Intersect(Range("john"), Range("apple")).Value + Intersect(Range("john"), Range("pear")).Value

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,621

    Re: How to use named ranges in VBA?

    Some examples:

    Option Explicit
    
    Sub Test()
    
    Range("J2:J4").Value = Evaluate("=apple+pear")
    Range("J7").Value = Evaluate("=sue apple")
    
    Debug.Print Range("apple").Address
    Debug.Print Range("orange").Address
    Debug.Print Range("pear").Address
    
    Range("K2:K4").Value = Evaluate("=" & Range("apple").Address & "+" & Range("pear").Address)
    Range("K7").Value = Evaluate("=" & Range("sue").Address & " " & Range("apple").Address)
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,621

    Re: How to use named ranges in VBA?

    And with mgs73's suggestions added:

    Option Explicit
    
    Sub Test()
    
    Range("J2:J4").Value = Evaluate("=apple+pear")
    Range("J7").Value = Evaluate("=sue apple")
    
    Debug.Print Range("apple").Address
    Debug.Print Range("orange").Address
    Debug.Print Range("pear").Address
    
    Range("K2:K4").Value = Evaluate("=" & Range("apple").Address & "+" & Range("pear").Address)
    Range("K7").Value = Evaluate("=" & Range("sue").Address & " " & Range("apple").Address)
    
    Debug.Print Intersect(Range("john"), Range("apple")).Value
    Debug.Print Intersect(Range("john"), Range("apple")).Value + Intersect(Range("john"), Range("pear")).Value
    Debug.Print Application.WorksheetFunction.Sum(Intersect(Range("john"), Union(Range("apple"), Range("pear"))))
    
    End Sub

  8. #8
    Registered User
    Join Date
    03-24-2024
    Location
    staines uk
    MS-Off Ver
    2000
    Posts
    3

    Re: How to use named ranges in VBA?

    Thank you for your replies.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2405 (Windows 11 23H2 64-bit)
    Posts
    81,434

    Re: How to use named ranges in VBA?

    Administrative Note:

    Is your forum profile showing the Excel PRODUCT that you need this to work for?

    Members will tailor the solutions they offer to the Office PRODUCT (Excel, NOT Windows) that you have. Please check that your forum profile is up-to-date in this respect. If you aren't sure, in Excel go to File | Account and report what it says below the MS logo at the top of that page. If your product is for Mac, please also state this.

    The three most recent Excel products are Excel 2019, Excel 2021 and MS365 - if you are using MS365, please give this name along with the version number in your profile (e.g. MS365 Version 2306). This is in the About Excel section further down the Account page.

    Thanks.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,621

    Re: How to use named ranges in VBA?

    @markringer: You're welcome.



    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,621

    Re: How to use named ranges in VBA?

    Thanks 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. 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. Replies: 1
    Last Post: 03-21-2006, 06:40 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