+ Reply to Thread
Results 1 to 15 of 15

VBA Create Dynamic Named Range

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Create Dynamic Named Range

    Hi, I wonder whether someone may be able to help me please.

    I'm using the code below to create a sheet and add named ranges to each column.

    Please Login or Register  to view this content.
    The problem I have is that the ranges are not dynamic i.e the end of the range is row 65536, rather than the last row containing data.

    I just wondered whether someone may be able to look at this please and offer some guidance on how I may go about adapting this so the last row of the range is the last row containing data?

    Many thanks and kind regards

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Create Dynamic Named Range

    Maybe of the newsheet has data:

    Please Login or Register  to view this content.
    If the newsht has no data, then you need to create dynamic formulas using OFFSET() that will expand automatically as you add data to the new sheet.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA Create Dynamic Named Range

    I think it is this line right here that is messin you up:

    Please Login or Register  to view this content.
    Try this instead

    Please Login or Register  to view this content.
    Note: It would be a good idea to dim all your variables

  4. #4
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @stnkynts, thank you for taking the time to reply to my post and for the code change suggestion.

    I've tried your suggestion, and unfortunately, although the code runs without errors, the range only extends from the header row to the next three rows of data, rather than the full range, so I'm not really sure where I've gone wrong.

    Many thanks and kind regards

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @JBeaucaire, thank you for taking the time to reply to my post and for your suggested approach.

    Many thanks and kind regards.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Create Dynamic Named Range

    Which column in your data set always has data in it? Use that column to search for the "Last Row" and edit the LR = part of the code I added to use that column instead of column A like I did.

  7. #7
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @JBeaucaire, thank you very much for your continued help with this. I've made the changes as you suggest, but unfortunately this doesn't work. The range that is created for each column only contains the cell reference for the header row.

    Many thanks and kind regards

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VBA Create Dynamic Named Range

    What data is on the active sheet when the code runs?
    If posting code please use code tags, see here.

  9. #9
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @Norie, thank you for taking the time to reply to my post.

    The only data on the sheet when the code runs are the column headings. I know this is probably very much a beginners question, but I was under the impression that a dynamic range expands to accommodate any data once the range has been set. Could you tell me please, is this not the case?

    Many thanks and kind regards

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VBA Create Dynamic Named Range

    If you want dynamic named ranges that expand then you would need to use formulas.

    What you are creating are static named ranges and as far as I know the only way they will expand is if you insert rows/columns in them.

    By the way, why are you using the last row of the sheet that is active at the start of the code for ranges on newly added sheets?

  11. #11
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @Norie, thank you for coming back to me so quickly this and for the info on the dynamic ranges.

    In respect of the 'Last Row' issue, you probably won't have had chance to see the earlier posts, but his was a suggestion put forward to me earlier on in the thread. Unless I've misunderstood, my original code didn't include this.

    Many thanks and kind regards

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: VBA Create Dynamic Named Range

    What ranges do you want to create on the new sheet?

    Have you considered using tables?

  13. #13
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @Norie, yes I had thought of that. Unfortunately, although I use Excel 2013 at home, my employers currently use 2003.

    It looks like I'll have to go away and think about this a little more.

    Thank you very much for your time and trouble.

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Create Dynamic Named Range

    The five Dynamic named formulas I would use would be:

    MPList: =OFFSET('Monthly Projects'!$B$5,,,COUNTA('Monthly Projects'!$B$5:$B$50000),)
    MPResourceLOB: =OFFSET('Monthly Projects'!MPList,,1,,)
    MPFFTE: =OFFSET('Monthly Projects'!MPList,,2,,)
    MPAFTE: =OFFSET('Monthly Projects'!MPList,,3,,)
    MPCap: =OFFSET('Monthly Projects'!MPList,,4,,)

    The first, MPList, counts the values in column B from B5 down and creates a list that many rows long. All the other named ranges simply offset from the MPList range by one column to the right.

    Now, VBA adds those same formulas to the sheet for you like so:
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Dynamic Named Range

    Hi @JBeaucaire, thank you very much for your continued help with this and the time spent putting the script together, it is greatly appreciated.

    All the best and kind regards

+ 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. Macro to create a Dynamic named range.
    By Hilton1982 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-28-2013, 09:42 AM
  2. [SOLVED] Create Multiple Dynamic Named Ranges with a Macro
    By bennank in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-09-2012, 12:55 PM
  3. Create a dynamic range on named cell?
    By garyi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-03-2011, 04:06 PM
  4. Named ranges-Should I use a dynamic named range
    By foseco in forum Excel General
    Replies: 4
    Last Post: 06-11-2009, 03:56 PM
  5. [SOLVED] Can you create dynamic named ranges containing array formulas?
    By Marie J-son in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2006, 09:10 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