Consulting

Results 1 to 7 of 7

Thread: Solved: Macro auto change name in VBE

  1. #1

    Solved: Macro auto change name in VBE

    Hi. I would like to know if there's such a thing as a macro to change the name in the VBE.

    The thing is that when I change the name in the tab of the worksheet, the name in the VBE doesn't change as well. When I run the codes, it could not detect the sheet as its name is different from the sheet & in the VBE (Properties - Alphabetic - Name). I've to change it manaually, which can be qte troublesome for those who know nothing about VBEs.

    Can anyone suggest some way or VBA codes to get such a macro done? Thanks!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule.Name = "somevalue"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Johnske has a great article here that will help you understand what is going on in referencing your worksheets.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Thank u, xld & lucas! Your help is much appreciated

  5. #5
    Quote Originally Posted by xld
    [vba]

    ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule.Name = "somevalue"
    [/vba]
    xld,

    I ran into this:

    Run-time error '1004' Programmatic access to Visual Basic Project is not trusted. How can I resolve this error?

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Under Tools | Macro | Security | Trusted Publishers you need to tick "Trust access to Visual Basic Project".

    Having said that, I don't think you actually need it. Let's think about what you are trying to do. If your sheet tab is called "Tax", you can reference that sheet in your code by using

    [vba]Sheets("Tax").Range("A1")[/vba]

    However the name shown on the tab can be changed at any time. If your code uses the reference above and the name is changed on the tab, your code will generate an error.

    In the VBE window, let's now assume that you have assigned the name "shTax" to your sheet. To reference that sheet in your code, you would use

    [vba]shTax.Range("A1")[/vba]

    Regardless of the name that you see on the sheet tab, your code will continue to work because the name in the VBE window has not changed.

    Whilst XLD's code will change the name of the sheet in the VBE window for you, every place in your code where you reference "shTax" is now wrong, because that sheet name no longer exists. In effect, what you are doing is removing any benefit of giving the sheet a name in the VBE window in the first place.

    Try the attached sample. Run both macros, and note the message that displays. Then change the name of the sheet tab, and run both macros again.

  7. #7
    Thanks Geekgirlau. I now see the difference & its effects. Thanks for sharing the knowledge

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •