Consulting

Results 1 to 7 of 7

Thread: Solved: How do I change the name of a sheet in VBA properties with VBA

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: How do I change the name of a sheet in VBA properties with VBA

    I know how to change the name of a sheet manually in properties but can I change it in with code???

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi there,

    These should work ...

    [vba]ThisWorkbook.VBProject.VBComponents("Sheet1").Name = "NewName"
    'or
    CallByName ThisWorkbook.VBProject.VBComponents("Sheet1"), "Name", VbLet, "NewName"
    'or
    Public Sub SetNamedProperty(objTarget As Object, strPropName As String, NewVal As Variant)
    With New TLIApplication
    .InvokeHook objTarget, strPropName, INVOKE_PROPERTYPUT, NewVal
    End With
    End Sub
    Sub ChangeCodeName()
    Call SetNamedProperty(ThisWorkbook.VBProject.VBComponents(ThisWorkbook.Worksheet s(1).CodeName), "Name", "NewName")
    End Sub
    'or
    Dim wksTest As Worksheet
    Set wksTest = ThisWorkbook.Worksheets(1)
    wksTest.[_CodeName] = "NewName"[/vba]

    Make sure that the workbook you want to perform this on is substituted for ThisWorkbook, unless the workbook you want to change is housing the code.

    HTH

  3. #3
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Yes
    Sheets("Sheet1").Name = "Test"
    changes the name to Test.
    You could have just recorded a macro of you doing this of course.

    Did I miss something in that question??

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    OBP yes but if you look at the properties for that sheet in the VBA editor its name will still be Sheet1 or whatever sheet number it was. For code I just wrote recently it didn't work unless the two names matched up.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Djblois
    OBP yes but if you look at the properties for that sheet in the VBA editor its name will still be Sheet1 or whatever sheet number it was. For code I just wrote recently it didn't work unless the two names matched up.
    Do you understand the difference between Excel sheet name and the worksheet codename?

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    No what is the difference???

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    All worksheets have two names, the Excel name, and the codename.

    When you go to the VBIDE, and select a worksheet, you will see it is called something like

    Sheet1 (Sheet1)

    The first is codename, the second is the Excel sheet name. If you change the name of the worksheet in Excel, say to Data, the entry in the VBIDE would then be

    Sheet1 (Data)

    The benefit of this is that it gives the VBA programmer a handle to the worksheet that will not be affected if a user chooses to change the worksheet name in Excel.

    So instead of saying

    Worksheets("Data").Range("A1").Value = "xxx"

    you can say

    Sheet1.Range("A1").Value = "xxx"

    and rest assured in the knowledge that it sould always be that name.

    You can also change the codename yourself, in the VBIDE if you select the worksheet, and make sure the properties window is open (F4), you will see two name properties, The first, (Name), refers to the codename, the second, Name, refers to the Excel name. You can replace the codename with a name of your choice, it is a good idea to give it a meanigful name.

Posting Permissions

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