+ Reply to Thread
Results 1 to 22 of 22

Sheet codenames work in array but not named range

  1. #1
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Sheet codenames work in array but not named range

    I would be grateful if someone could tell me why this works:

    Please Login or Register  to view this content.
    but this doesn't (throws "Object doesn't support this property or method"):

    Please Login or Register  to view this content.
    I am guessing that the array is passing the sheet codenames as objects and that's what is needed. If so, what do I need to do to get the same sheet codenames held in the named range "MainSheets" to work in the same way? I have tried reading "MainSheets" into an array but that doesn't work.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Sheet codenames work in array but not named range

    See if this works for you,


    Please Login or Register  to view this content.
    EDIT: refers to sheet names, will look further into codenames..
    Last edited by berlan; 04-11-2016 at 05:01 PM.

  3. #3
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: Sheet codenames work in array but not named range

    have you tried
    Please Login or Register  to view this content.
    ?
    Last edited by bulina2k; 04-11-2016 at 05:10 PM.
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Sheet codenames work in array but not named range

    What is Mainsheets? is that a named range on your spreadsheet?

    If so that is a text string not a sheet.

    Your code is trying to find a sheet

    I use:-

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Quote Originally Posted by bulina2k View Post
    have you tried
    Please Login or Register  to view this content.
    ?
    Thanks, but this only works with sheet names not codenames.

  6. #6
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Quote Originally Posted by mehmetcik View Post
    What is Mainsheets? is that a named range on your spreadsheet?

    If so that is a text string not a sheet.

    Your code is trying to find a sheet

    I use:-

    Please Login or Register  to view this content.
    Thanks for replying. Yes, "MainSheets" is a named range in the workbook, and contains a list of some worksheets by codename I want to set as visible or not visible. As you say, they are strings so it doesn't work. I think they would need to be converted to objects, but I'm not sure VBA can do that.

    What I don't understand is that if I put the sheet codenames into an array (eg Array(Sheet01, Sheet02, Sheet03) what I want to do works. Your code only seems to work on sheet names, not code names. The following code fails with the compile error "Invalid qualifier".

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Quote Originally Posted by berlan View Post
    EDIT: refers to sheet names, will look further into codenames..
    Thanks, that is the challenge! What I don't understand is why putting the sheet codenames into an array (eg Array(Sheet01, Sheet02, Sheet03) works.

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

    Re: Sheet codenames work in array but not named range

    Array(Sheet01, Sheet02, Sheet03) is actually an array of sheet objects, not just the names.

    How have you defined your named range?
    If posting code please use code tags, see here.

  9. #9
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Further information. As I have said, putting the sheet codenames into an array (eg Array(Sheet01, Sheet02, Sheet03) works. I don't want to do this because several subs need to access a common list which I can edit if things change. So the question really is: how can I maintain only one array several subs can access.

  10. #10
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Thanks for replying.

    Quote Originally Posted by Norie View Post
    Array(Sheet01, Sheet02, Sheet03) is actually an array of sheet objects, not just the names.
    That's what I suspected. Even that took me some time to get right!

    Quote Originally Posted by Norie View Post
    How have you defined your named range?
    With name manager (RefersTo: =Workings!$Z$2:$Z$17). The cells themselves are formatted to text. Perhaps there is a cell format that would make it work?

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Sheet codenames work in array but not named range

    I wouldn't so like this for myself though...

    Perhaps,
    You need to tick "Trust access to the VBA project object model" in "Macro Settings tab" in "Option".
    Please Login or Register  to view this content.

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

    Re: Sheet codenames work in array but not named range

    You could create a function than when passed the codenames as strings in an array - which is what I think you have in your named range - it returns an array of sheet objects.

  13. #13
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Quote Originally Posted by jindon View Post
    You need to tick "Trust access to the VBA project object model" in "Macro Settings tab" in "Option".
    Please Login or Register  to view this content.
    I can't remember why, but I have already ticked "Trust access to the VBA project object model" so your code works!

    Quote Originally Posted by jindon View Post
    I wouldn't so like this for myself though...
    Why? Is "Trust access to the VBA project object model" dangerous?

    Thank you for replying.

  14. #14
    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,447

    Re: Sheet codenames work in array but not named range

    A little inelegant but try this:

    Please Login or Register  to view this content.
    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


  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Sheet codenames work in array but not named range

    I mean, I would use sheet names not codenames...

  16. #16
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Quote Originally Posted by Norie View Post
    You could create a function than when passed the codenames as strings in an array - which is what I think you have in your named range - it returns an array of sheet objects.
    I had a feeling the solution might be something like that. Unfortunately I struggle with functions and arrays so any clues would be gratefully received.

    Jindon has suggested the code below that works but it requires "Trust access to the VBA project object model" to be enabled and he/she wouldn't use it themselves, but I don't know why.

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    Quote Originally Posted by jindon View Post
    I mean, I would use sheet names not codenames...
    I understand why, but names might need changing whereas the codename can always remains the same. Thanks for your suggestion though, which works.

  18. #18
    Registered User
    Join Date
    11-27-2011
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Sheet codenames work in array but not named range

    [QUOTE=TMS;4361664]A little inelegant ...

    ...but it works!

    I was thinking comparing the named range to ThisWorkbook.Sheets was the way forward, but kept going down blind alleys. Thank you very much.

  19. #19
    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,447

    Re: Sheet codenames work in array but not named range

    You're welcome. Thanks for the rep.

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Sheet codenames work in array but not named range

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

  21. #21
    Forum Contributor
    Join Date
    06-02-2015
    Location
    delhi
    MS-Off Ver
    2010
    Posts
    104

    Re: Sheet codenames work in array but not named range

    Please Login or Register  to view this content.

  22. #22
    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,447

    Re: Sheet codenames work in array but not named range

    @Mandeep Baluja: Late entry but that only works for sheet (tab) names, not Sheet CodeNames.

+ 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. Using codenames to overcome sheet name changes
    By ken_e in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2014, 07:41 AM
  2. [SOLVED] Evaluate Named Range Array formula to VBA Array
    By cmore in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-07-2014, 09:06 PM
  3. [SOLVED] How to reference a sheet with CodeNames and variables
    By Ed_Collins in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-16-2013, 12:48 PM
  4. Array formulas referencing a named range or named table
    By anrichards22 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-03-2013, 11:59 PM
  5. [SOLVED] Read range into an array, work with it, then paste it back to a sheet.
    By wolis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2012, 10:07 AM
  6. Use of Sheet CodeNames to Select Sheet in Different Workbook
    By Randy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-14-2005, 12:05 AM
  7. Replies: 1
    Last Post: 06-09-2005, 08:05 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