Results 1 to 20 of 20

Thread: [RESOLVED] Renaming Worksheets

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Resolved [RESOLVED] Renaming Worksheets

    I'm building a schedule in Excel with each worksheet tab indicating a particular day. My code keeps a date in one cell on each worksheet. I've been trying to write code that will rename the worksheet to exactly what is in this date cell, and nothing I've tried works. I keep getting error messages. Not even code from the macro recorder works. Does anyone know some code that will rename a worksheet to whatever is in a particular cell?

    Thanks,

    Danno

  2. #2
    Addicted Member Lemon Lime's Avatar
    Join Date
    Jul 2000
    Location
    Space, the final frontier, go along the yellow brick road,turn left then left again. In the service window, ask for the insane dude.
    Posts
    167

    Re: Renaming Worksheets

    Hey Danno, Welcome to VBForums!!!

    if your object is ("excel.worksheet") just
    VB Code:
    1. ExcelSheet.worksheets(1).Name = "whatever"

    have a nice day!!
    I've had enough with sainity!
    What's the use of it anyway?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: Renaming Worksheets

    Yes, I understand the classic naming of a worksheet. My problem is: I need to name the worksheet to exactly what is in a particular cell in each worksheet. In my project, cell A23 holds a date. I need each worksheet to be named the date that is in cell A23. I can get it to work with the last worksheet in the workbook with the (sheets.count) code, but I can't get it to work with any other worksheet. Frustrating...

    Danno

    PS: Thanks for the welcome

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Renaming Worksheets

    This might give you some ideas

    VB Code:
    1. Dim oExcel As Excel.Application
    2.     Dim oBook As Excel.Workbook
    3.     Dim oSheet As Excel.Worksheet
    4.    
    5.     Set oExcel = New Excel.Application
    6.     Set oBook = oExcel.Workbooks.Open("C:\projects\testing.xls")
    7.     oExcel.Visible = True
    8.    
    9.     For Each oSheet In oBook.Worksheets
    10.         'assumes cell A23 is a valid date
    11.         'The Sheet Name cannot contain the characters \ / ? * [ ]
    12.         oSheet.Name = Format$(oSheet.Cells(23, 1), "dd-MMM-yyyy")
    13.     Next
    14.    
    15.     oBook.Close True
    16.     Set oBook = Nothing
    17.    
    18.     oExcel.Quit
    19.     Set oExcel = Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: Renaming Worksheets

    It doesn't like this line of your code:

    oSheet.Name = Format$(oSheet.Cells(23, 1), "dd-MMM-yyyy")

    It produces the same error messages I've been getting about bad spelling and/or illegal characters in the worksheet name. The weird thing is that it will work if all you want to do is rename the last worksheet because you can use the (sheet.count) code. Excel just seems to reject the automatic renaming of any other sheet except the last one. I know I'm doing something wrong, just don't know what.

    Danno

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Renaming Worksheets

    What is the data in cell - oSheet.Cells(23, 1)? If its a blank string or non-date type data then you will get the error.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: Renaming Worksheets

    I used this to rename worksheets in an old project of mine.
    H3 is used to store the date in a text format (from A23) before being deleted
    VB Code:
    1. ' Rename New Sheet to dd-mmm
    2. Range("H3").Formula = "=Text(A23, ""dd"" & ""-"" & ""mmm"")"
    3. ActiveSheet.Name = Range("H3")
    4. Range("H3").Formula = ""

    adding the year should be easy enough. ..mm"" & ""-"" & ""yyyy"")"

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: Renaming Worksheets

    Thank you LexII... your code worked!!

    I've added to your code to customize mine to exactly what I need.

    Thanks once again...Problem Resolved!!

    Danno

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Renaming Worksheets

    Now that your thread is solved dont forget to 'Resolve' your original post so other members can see from the forum view level that its Resolved.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Renaming Worksheets

    Danno ... what version of Excel are you running? Everything I have tried (without having to use an intermediary cell) in 2003 works just fine.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  11. #11
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Renaming Worksheets

    WebTest, I think the problem is coming from the way excel stores the value in a cell.
    All cells that are DATE stores the value like this 28/02/06. Even if the cell display 28-02-06 or 28-feb-06 or whatever, the data is stored like 28/02/06 in the cell.

    So if you ask excel to rename a sheet with the value of a cell that is DATE, you've got an error because sheet name cannot have "/" in it.

    This is why Danno got error and you don't.

  12. #12
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Renaming Worksheets

    Quote Originally Posted by billhuard
    All cells that are DATE stores the value like this 28/02/06. Even if the cell display 28-02-06 or 28-feb-06 or whatever, the data is stored like 28/02/06 in the cell.

    So if you ask excel to rename a sheet with the value of a cell that is DATE, you've got an error because sheet name cannot have "/" in it.
    Thanks, but Danno said the following:
    Quote Originally Posted by Danno
    It doesn't like this line of your code:

    oSheet.Name = Format$(oSheet.Cells(23, 1), "dd-MMM-yyyy")
    ... which does NOT have any "/" characters in it ... this function works for me just fine.
    Last edited by Webtest; Feb 28th, 2006 at 10:32 AM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  13. #13
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Renaming Worksheets

    You're right
    the function worked fine for me too.

    better roll my tongue 7 times before writing next time

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: [RESOLVED] Renaming Worksheets

    To Webtest and billhuard,

    I am still stuck with Excel 2000. billhuard is absolutely correct. What I didn't know is that even if you store the date in a cell correctly (no illegal characters), Excel still stores the date with a / as the divider which is an illegal character for renaming worksheets. I'm glad to hear that this is not a problem in Excel 2003, maybe my company will spring for it now.

    I'm pretty new to VBA and it is just the coolest thing to see what you can do to Excel with some VBA code. The more I learn the more intoxicated with power I become!!

    Thanks again one and all.

    Danno

  15. #15
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [RESOLVED] Renaming Worksheets

    Danno ...

    I don't think it is a problem with Excel 2000. I tried a few things in Excel '97 AND Excel 2003 ... here are the results ...
    Code:
    'Cells(1,1) contains value 3/4/2006 display formatted as 3-Mar-06
    
    'BOTH OF THE FOLLOWING RUN FINE IN XL-'97 AND XL-2003
    ActiveSheet.Name = Format$(ActiveSheet.Cells(1, 1).Value, "dd-MMM-yyyy")
    ActiveSheet.Name = Cells(1, 1).Text   '<Here is one place where Value and Text are DIFFERENT!!!
    
    'FAILS due to illegal characters in Sheet Name
    ActiveSheet.Name = Cells(1, 1).Value
    Yes, VBA is very powerful. The book "Professional Excel Development" by Bullen, Bovey, & Green (Addison Wesley) is dedicated to building amazing Graphic Form based applications on the same order as Visual Basic applications. This book proclaims that Excel is "not just spreadsheets", but a rich development platform. It really digs into the buried power of VBA running under Excel.

    Good Luck and Good Programming!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Renaming Worksheets

    .Text and .Value can hold different values so make sure they are the same.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: [RESOLVED] Renaming Worksheets

    Webtest,

    Thanks for the code and for the book recommendation. I'll get it ordered today.

    Danno

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: [RESOLVED] Renaming Worksheets

    RobDog888,

    You're right about .value and .text. I'll be careful


    Danno

  19. #19
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [RESOLVED] Renaming Worksheets

    If you are interested in the "nuts and bolts" technical details of Excel VBA, another reference that I like is:

    "Definitive Guide to Excel VBA"
    Michael Kofler
    a! Press

    Neither of the books that I've mentioned will answer all of your questions, but between them and the Forum, you have access to an amazing depth of knowledge!!!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    28

    Re: [RESOLVED] Renaming Worksheets

    Got it Webtest, I'll get that one too.

    Danno

Posting Permissions

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



Click Here to Expand Forum to Full Width