+ Reply to Thread
Results 1 to 11 of 11

Create Named Ranges on inactive sheets

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

    Create Named Ranges on inactive sheets

    old code which works but loops are bulky.
    Please Login or Register  to view this content.
    new code which despite sheet specified ranges tries to set the names to the active sheet.
    Please Login or Register  to view this content.
    Is there a way to work this new without using sheet.activate?

    Edit: The issue is not that the code fails to execute. It is just not quite doing what I want VPM.Names.Add or DMU.Names.Add create sheet specific named ranges but VPM.Range.CreateNames appears to be ignoring the worksheet and just passing the addresses to the activesheet named ranges.
    Last edited by Leon V (AW); 05-20-2019 at 09:45 AM.
    Sub Reputation()
    Dim Problem as Variant
    Dim Reputation as Integer
    For Each Problem in Forum.Threads
        If Problem.Title = "*[Solved]*" and Solver.Name = "Leon V (AW)" Then Reputation = Reputation + 1
    Next Problem
    End Sub

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

    Re: Create Named Ranges on inactive sheets

    This works for me on an inactive sheet
    Please Login or Register  to view this content.
    how are you declaring vpm?

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

    Re: Create Named Ranges on inactive sheets

    Yes the VPM line works fine because it is first. It is the DMU line that has issues because it pops up the "would you like to replace named ranges" box.
    VPM and DMU are set as a public variable in the calling macro.
    I should have mentioned that VPM and DMU have the same headers.
    Please Login or Register  to view this content.

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

    Re: Create Named Ranges on inactive sheets

    CreateNames creates named ranges that are workbook in scope & as far as I know that cannot be changed.
    To create named ranges that are scoped to the sheet I think that you will have to stick with the loop.

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

    Re: Create Named Ranges on inactive sheets

    Can VBA change the scope of named ranges after they have been created?
    I would be far more comfortable looping through a list of name objects than looping through range objects.

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Create Named Ranges on inactive sheets

    Quote Originally Posted by Leon V (AW) View Post
    I would be far more comfortable looping through a list of name objects than looping through range objects.
    Why? You would just be adding an unnecessary step.
    Rory

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

    Re: Create Named Ranges on inactive sheets

    You could use something like
    Please Login or Register  to view this content.
    But you will have to do this on the Vpm sheet, then create the names on the Dmu sheet.
    Also those sheets MUST NOT be active

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

    Re: Create Named Ranges on inactive sheets

    Quote Originally Posted by rorya View Post
    Why? You would just be adding an unnecessary step.
    looping through ranges is very sensitive to user settings like R*C vs ?# or step to next row vs next column. That's why I make these named ranges, to avoid range referencing later in the code.
    I know in this case it would be little difference (auto create and loop through vs loop through and create) however I still like to avoid bad habits where possible.

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Create Named Ranges on inactive sheets

    That makes no sense to me. You're processing the same ranges (referred to the same way) either way in this code, you're just adding unnecessary steps.

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

    Re: Create Named Ranges on inactive sheets

    I'm with rorya on this.
    It's better to correctly name & reference the range when created than loop through them afterwards changing there scope.
    Whilst I have no idea what the structure of your workbook is, with the code I supplied above where Vpm is Sheet2.
    If you have named ranges on a sheet called Sheet21, they would also be changed to sheet scope.

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

    Re: Create Named Ranges on inactive sheets

    Quote Originally Posted by Fluff13 View Post
    I'm with rorya on this.
    It's better to correctly name & reference the range when created than loop through them afterwards changing there scope.
    Whilst I have no idea what the structure of your workbook is, with the code I supplied above where Vpm is Sheet2.
    If you have named ranges on a sheet called Sheet21, they would also be changed to sheet scope.
    There will only be 2 sheets (VPM & DMU) created by the add-in in a new document, both of them will have overlapping header names so all of them need to be sheet scope named ranges. Better the devil I know I guess, it's just really annoying when VBA can do 95% of what you want (almost capable is more frustrating than incapable).

+ 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] How to define, refer to and reference ranges on inactive sheets
    By TFiske in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-14-2018, 11:44 PM
  2. [SOLVED] Create named ranges using the first row
    By alice2011 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-20-2015, 12:18 PM
  3. Use list to create named ranges
    By gerenrich in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-31-2014, 04:29 AM
  4. VBA To Create Named Ranges
    By tschuman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-20-2013, 03:07 PM
  5. Excel 2007 : Using named ranges to create charts
    By EUROSTAT in forum Excel General
    Replies: 7
    Last Post: 01-09-2010, 09:13 PM
  6. Create named ranges
    By loopoo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-22-2005, 09:16 AM
  7. Using VB to to create named ranges
    By PC in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-19-2005, 06:06 AM

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