+ Reply to Thread
Results 1 to 13 of 13

Using codenames in VBA?

  1. #1
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Using codenames in VBA?

    From what I’ve learned about the pros/cons of using codenames in VBA, I’m inclined to use them. But I would appreciate any comments pro or con from the experts here that feel strongly on that matter.
    I am having a problem using codenames to qualify a range however. In the Immediate Pane of the VBA editor,

    ?activesheet.codename returns cnEarnFlags

    cnEarnFlags.Range("templaterow").Copy will copy the proper range, but

    activesheet.codename.Range("templaterow").Copy returns a Run-time error 424 Object required. Since this is only a substitution/combination of the first two commands, I thought it would work.

    I need this code in the “Thisworkbook” page of my workbook. Appreciate any ideas. Thanks.

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Using codenames in VBA?

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

    Never mind, it doesn't work.
    Last edited by leelnich; 08-04-2017 at 10:56 PM.

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

    Re: Using codenames in VBA?

    Why codename for ActiveSheet?
    Please Login or Register  to view this content.
    See my posts in http://www.ozgrid.com/forum/showthread.php?t=175785

  4. #4
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Using codenames in VBA?

    leelnich - Thanks for the reply. That gives me a Run-time error 9 - Subscript out of range

  5. #5
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Using codenames in VBA?

    jindon – I have a dozen worksheets which will use the same code. I’m using the codename to differentiate which worksheet triggered the code.
    Please Login or Register  to view this content.
    I was testing using the Activesheet – the actual code will use the Sh object above. But it still throws the same error.

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

    Re: Using codenames in VBA?

    Have you read the link?
    Please Login or Register  to view this content.

  7. #7
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Using codenames in VBA?

    Codenames are just a fairly "stable" way to specify sheets when you're WRITING the code. You use them to make sure you're processing the correct sheets. If you're writing generic code intended to work with multiple compatible sheets, you'll probably refer to worksheets using either the ActiveSheet property or variables which are defined at run-time. Codenames aren't needed in either case.
    Last edited by leelnich; 08-04-2017 at 11:48 PM.

  8. #8
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Using codenames in VBA?

    Yes, but I didn't see how it applied. I just looked at it again, and am still not sure how to use it. Perhaps I didn't explain my question well. All of the codenames above "cnXXX" have a range named "templaterow" defined as a workheet level name. Currently I have the same code in each worksheet in a SelectionChange sub which I'm trying to consolidate into the Thisworkbook object (i.e. 13 copies of the same code into 1). Also trying to use the codename property vs. the worksheet name. This is the worksheet code I'm trying to replace from one of the individual worksheets:
    Please Login or Register  to view this content.
    Does this help explain my question?

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

    Re: Using codenames in VBA?

    Again, have tried the code I have posted?
    If you know how to set the worksheet using CodeName, the rest should be very easy.
    Please Login or Register  to view this content.
    Then you can after the Select statement
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Using codenames in VBA?

    Did you post the link you intended? The code on the link you provided adds a new worksheet my workbook (not what I'm trying to do) and does not include a Select Case statement. Sorry, maybe I'm just completing misunderstanding you, but now I have tried your code and am still confused. I appreciate your effort.

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

    Re: Using codenames in VBA?

    Quote Originally Posted by jindon View Post
    Have you read the link?
    Please Login or Register  to view this content.
    Above is the code I was referring to.

  12. #12
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Using codenames in VBA?

    I apologize - I must be more tired than I thought - I missed that post. I have added it now and it's throwing a 1004 Error "Programmatic access to Visual Basic Project is not trusted"

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

    Re: Using codenames in VBA?

    See the link and follow the steps for the article

    "To enable or disable access to Visual Basic Projects"

    https://msdn.microsoft.com/en-us/library/tt6cbe57.aspx
    Last edited by jindon; 08-05-2017 at 12:38 AM.

+ 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. Sheet codenames work in array but not named range
    By StevePM in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 04-12-2016, 08:43 AM
  2. 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
  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. Dealing with sheets codenames
    By sprites in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-08-2013, 09:33 AM
  5. [SOLVED] Using worksheet codenames to hide multiple worksheets.
    By stuandrews1618 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-19-2013, 01:58 PM
  6. Using a variable to gain access to worksheets through codenames.
    By Kozak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-10-2006, 12:30 PM
  7. using variables in codenames
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2006, 06:35 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