+ Reply to Thread
Results 1 to 15 of 15

Macro for renaming a worksheet who's name changes...

  1. #1
    Registered User
    Join Date
    01-18-2005
    Posts
    10

    Macro for renaming a worksheet who's name changes...

    Ok, so here's the deal. I'm trying to make a macro, that in part renames an existing worksheet. Before this macro is run, the worksheet is the only worksheet that exists in the file. The only problem is the worksheet's name changes depending on which file I open. And everytime I record the macro, the VB code comes out as

    "Sheets("SheetABC").Select
    Sheets("ABC").Name = "newname"

    The problem is in other files that this macro needs to be used on, the sheet is named BCA or XYZ or whatever. so the macro goes to select sheetABC but it's not there, because it has a different name, and I get an error message.

    What the heck would the code be to make Excel always select the 1 worksheet that exists in each file, and rename it to 'newname'???

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Quote Originally Posted by Accipiter
    Ok, so here's the deal. I'm trying to make a macro, that in part renames an existing worksheet. Before this macro is run, the worksheet is the only worksheet that exists in the file. The only problem is the worksheet's name changes depending on which file I open. And everytime I record the macro, the VB code comes out as

    "Sheets("SheetABC").Select
    Sheets("ABC").Name = "newname"

    The problem is in other files that this macro needs to be used on, the sheet is named BCA or XYZ or whatever. so the macro goes to select sheetABC but it's not there, because it has a different name, and I get an error message.

    What the heck would the code be to make Excel always select the 1 worksheet that exists in each file, and rename it to 'newname'???

    Hello Accipiter,

    Very good question. A worksheet is part of the worksheets collection. Every collection wether in VB or VBA can be retrieved in 2 ways. Either by it's string name as in "ABC" or it's place within the collection, which is a unique number.

    See where I'm going with this? Since you only have the one sheet in each workbook, write your code so it uses the object collection index number instead of it's string name.

    For example:

    Worksheets("SheetABC") is the same as Worksheets.Item(1)

    References what you want and you don't have to worry about what someone else has named it.

    Hope this answers your question,
    Leith Ross

  3. #3
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    So I could use the code Worksheets.Item(1).Name = "newname"...or Worksheets.(Sheet1).Name = "abc"

    i'm kinda hazy on what the code should actually be...

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Quote Originally Posted by Accipiter
    So I could use the code Worksheets.Item(1).Name = "newname"...or Worksheets.(Sheet1).Name = "abc"

    i'm kinda hazy on what the code should actually be...

    The beauty of this is you don't need to concern yourself with the sheet name. Just write your code as Worksheet.Item(1).

    Here is what a Collection looks like:

    Index String Name
    1 "Abc"

    Or think of it like an a String Array...

    Worksheet(1) = "Abc"

    Using the index we still reference the same object and don't have to worry about the name when accessing it.


    Hope this is clearer,
    Leith

  5. #5
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    it's definetly not working. I'm testing it out, this is the code I have for the macro:

    Sub Macro1()

    Worksheet(1) = "Abc"

    End Sub


    When I run it in one of the workbooks with only 1 worksheet that it needs to be used in i get this error:


    "Compile error: Sub or function not defined"

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Quote Originally Posted by Accipiter
    it's definetly not working. I'm testing it out, this is the code I have for the macro:

    Sub Macro1()

    Worksheet(1) = "Abc"

    End Sub


    When I run it in one of the workbooks with only 1 worksheet that it needs to be used in i get this error:


    "Compile error: Sub or function not defined"

    OK. The best thing to do is post the code and I'll edit the code so you can see what to do. Working with objects is tricky.

    Thanks,
    Leith

  7. #7
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    alright, thank you very much....Right now i have to get this macro to rename the workbook before i can go on to anything else. Maybe I'm missing something here, but this is all the code I have for it right now:



    Sub Macro1()

    Worksheet(1) = "Abc"

    End Sub

    Again, the person running it will open up a workbook, which will contain 1 worksheet, and then run the macro and have the worksheet be renamed to "Original" (that's the actual name it will bear, not 'newname'). The worksheet name will vary from workbook to workbook that is opened.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    My apologies, I misunderstood. To rename the Workbook do this...

    Sub Macro(1)

    Worksheets.Item(1).Name = "Abc"

    End Sub

    This will rename your sheet "Abc"


    Best,
    Leith Ross

  9. #9
    Registered User
    Join Date
    07-01-2004
    Location
    Belgium
    Posts
    1

    Exclamation

    Accipiter,

    It's logical the code doesn't work because it's not correct. The code should actually be:

    Sub Macro1()
    Worksheets(1) = "Abc"
    End Sub

    So Worksheets(1) instead of Worksheet(1), because that's the name the collection has. Normally, if you compile the project, Excel would point you to this problem.

    Regards,
    Bart

  10. #10
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    Leith this is driving me nuts, i tried your corrected code and i get the same error as before,

    Bart I try yours and i get "Compile Error: Wrong number of arguments or invalid property assignment"

    it actually highlights 'sub macro1()' in yellow instead of the code itself after i clear the error message...

    This is driving me crazy...lol..

    I'm positive the sheet i'm testing it on is called sheet1. The worksheet i've been testing it on is called "PYE_Partc2019....." when i click on the tab for it, then go to view code, under the VBAProject/Microsoft Excel Objects menu it says 'Sheet1(PYE_Partc2019...), so I don't think it's that.....
    Last edited by Accipiter; 01-18-2005 at 04:58 PM.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Well,

    Two things to check. 1) See if Macro1 is a reserved or keyword and 2) This code should be in a Project Module. Not in the Sheet Code.

    All I can think of.

    Best,
    Leith Ross

  12. #12
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    I tried both and I'm clear on both...thanks for your help though....have you ever actaully made a macro that does what I need to do? Maybe if you had one i could snag the code you used..

    I'm starting to think it can't be done, which is bizarre.
    Last edited by Accipiter; 01-18-2005 at 05:28 PM.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Your're right this is bizzare. To answer question about having done this before - Yes, many times. I have never encountered a problem like the one you having. Unless you're experiencing some system problems. When all else fails shutdown, run scandisk, disk cleaner, and defrag. Sorry, couldn't be more help. I know how frustrating programing can be. I've done it for a long time, 30 years now, 7 with just VBA.

    Repost if you figure this out. I would love know what it is.

    Sincerely,
    Leith Ross

  14. #14
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    still no luck...just to make sure...

    Sub Macro(1)

    Worksheets.Item(1).Name = "Abc"

    End Sub


    that's the code for the macro to rename the one worksheet in a workbook to 'Abc' when the macro is run?


    edit: uuuuuuuuuuugh apparently there's another macro on the computer called 'worksheets'.....i'm going rename it and see if this works....

  15. #15
    Registered User
    Join Date
    01-18-2005
    Posts
    10
    success!! thanks leith and bart, turns out once i renamed the old existing macro that was already present to 'wsheet' everything worked out fine....man that was a frustrating 5 hours..haha....it was even worse because i went home last night and it worked fine on my computer at home, then i cmae into work again today and still couldn't get it to work here...thanks man

    Worksheets.Item(1).Name = "Abc"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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