+ Reply to Thread
Results 1 to 11 of 11

Change Only Certain Worksheet Names Using a List

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Change Only Certain Worksheet Names Using a List

    I'm using code to rename worksheets based on input values in a difference worksheet. I do a simple link in cell Z1 to the list on my INPUTS worksheet, and the following code is on each worksheet.

    
    Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
        Set Target = Range("Z1")
        If Target = "" Then Exit Sub
        On Error GoTo Badname
        ActiveSheet.Name = Left(Target, 31)
        Exit Sub
    Badname:
        MsgBox "Please revise the project code on the INPUTS tab." & Chr(13) _
        & "It appears to contain one or more " & Chr(13) _
        & "illegal characters." & Chr(13)
        Range("A1").Activate
    End Sub

    I have this on the code for each sheet that needs to be changed. It works, but only after you click back on the worksheet after changing the cell value on my INPUTS worksheet. Is it possible to do it the other way around? VBA on the INPUTS sheet that will change the other worksheet names automatically when you change the cell value? And the names will change pretty often, so is there a way to mark these certain sheets so the code knows to rename them?

    I hope that makes sense.


    Thanks!!

  2. #2
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Change Only Certain Worksheet Names Using a List

    Hi there!
    I'm sure it can be done with Worksheet_Change event on the INPUTS worksheet.
    Please upload a dummy file with the minimum info.
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  3. #3
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: Change Only Certain Worksheet Names Using a List

    Ok...I've attached a sample file. You can see what I've done, and hopefully it makes sense what I'm wanting to do instead.

    BTW...I'm not familiar with the selection_change function. So I'll look into that now.

    Also, this is a workbook I'm passing off to other people, so simpler is always better, which is why I was hoping to automate.
    Attached Files Attached Files
    Last edited by phelbin; 02-03-2016 at 06:34 PM.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,178

    Re: Change Only Certain Worksheet Names Using a List

    As an outline design, use column A on the INPUT sheet for a list of the existing worksheet names and column B would, initially be blank ... but will be used for the new worksheet names.

    Then you need a Worksheet Change event handler monitoring the cells in column B. When a cell changes in column B, check if the sheet in the corresponding cell in column A exists. If it doesn't, put a warning in column C and exit. If it does, change the sheet name from its existing value to the new name. You should then add the new name to the bottom of the list in column A, and delete the old name from the list.

    Relatively straightforward. No code needed in any of the other sheets.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Change Only Certain Worksheet Names Using a List

    Even simpler! There is no need for 3 columns. You can take the old name (to be changed) with the selection_change event and change it on place.

  6. #6
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Change Only Certain Worksheet Names Using a List

    this code under INPUTS code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim new_name As String
    Dim ws As Worksheet
    
    If Target.Column = 2 And Target <> "" Then
        Set ws = Sheets(old_name)
        ws.Name = Target.Value
    End If
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 2 And Target <> "" Then
        old_name = Target.Value
    End If
    End Sub
    and this code in a module (insert Module)

    Option Explicit
    
    Public old_name As String
    Attached Files Attached Files
    Last edited by bulina2k; 02-03-2016 at 06:36 PM. Reason: attach the file

  7. #7
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: Change Only Certain Worksheet Names Using a List

    Bulina, that worked perfectly!! Thanks so much.

  8. #8
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Change Only Certain Worksheet Names Using a List

    Glad to help! Please mark the thread as [SOLVED]. (and add reputation if you wish)

  9. #9
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: Change Only Certain Worksheet Names Using a List

    Quick change. Can we limit the code to range B3:B7 instead of all of column 2?

  10. #10
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Change Only Certain Worksheet Names Using a List

    sure, replace both if target.column.... with
    If Not Intersect(Target, Range("B3:B7")) Is Nothing And Target <> "" Then

  11. #11
    Forum Contributor
    Join Date
    02-18-2014
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2013
    Posts
    197

    Re: Change Only Certain Worksheet Names Using a List

    Perfect! Thanks again for your help.

+ 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. Replies: 4
    Last Post: 07-24-2015, 08:33 AM
  2. List worksheet names in active worksheet By Excluding Specific Sheets
    By jagadeesh.rt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2014, 10:22 AM
  3. Macro to add worksheet - worksheet names change
    By PostID in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2013, 01:05 PM
  4. Change tab names from a list
    By RobertMika in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2011, 07:05 AM
  5. Replies: 5
    Last Post: 12-11-2010, 01:43 AM
  6. Replies: 0
    Last Post: 08-15-2008, 08:24 PM
  7. Code to List all worksheet names (tab names)
    By howard101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2007, 12:51 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