Solved

Renaming a sheet tab

Posted on 2014-10-22
12
128 Views
Last Modified: 2014-10-29
How do I create and new sheet tab, and name it with whatever the value is of the active cell?
I recorded the following and even with the "Use Relative References" on, it still used static values when I tried to copy and paste in the sheet tab renaming...

Sub Macro3()
    'I am starting the process with a cell selected that
    'will become the new sheet tabs name.  I copy the cell
    'and Ln3 is the paste into the sheet tab rename.

    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets("Sheet3").Select 'will be different each time
    Sheets("Sheet3").Name = "120-2758"  'will be different each time
    Sheets("ReadMe").Select
    ActiveCell.Rows("1:1").EntireRow.Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
End Sub

Open in new window

0
Comment
Question by:RWayneH
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40397932
I have a code exactly  as described, I will look tomorrow in my archive and will get back with solution .
0
 
LVL 33

Expert Comment

by:Norie
ID: 40397947
Put the new sheet name into a variable before you add the new sheet.
Dim wsNew As Worksheet 
Dim strShName As String

    strShName = ActiveCell.Value

    Set wsNew = Worksheets.Add

     wsNew.Name = strShName

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40397967
Had issues place those lines into the recorded code...   getting compile errors.
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 40398005
Sub AddSheet()
Dim wsNew As Worksheet
Dim strShName As String

    If ActiveCell.Value <> "" Then
        strShName = ActiveCell.Value
   
        Set wsNew = Worksheets.Add
   
        wsNew.Name = strShName
    Else
        Msgbox "Active cell is empty"
    End If
End Sub
0
 
LVL 33

Expert Comment

by:Norie
ID: 40398392
No compile errors when I tested the code, the only problem I can foresee is the active cell being empty or having an invalid sheet name.:)
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40398440
My code checks for blanks but imnorie makes a good point about invalid characters. I believe that the invalid characters are / ? * [ ] The name can also not exceed not exceed 31 characters.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Assisted Solution

by:RWayneH
RWayneH earned 0 total points
ID: 40399286
Thanks that will work....  here is what I landed on.  I added a For Selection so it would go through a list and create them all at once, however it fails if there is a dup.  How would I say that on error skip that and continue onto the next one.  The failure could be an invalid char or to many chars or a dup sheets tab....  etc.  

How would I add that to this?

Sub AddSheet()
 Dim wsNew As Worksheet
 Dim strShName As String


    For Each c In Selection  'C is a list is hightlighted in excel
    If c.Value = "" Then Exit For  'when end of list is reached, exit. no End If with the Then statement

    If ActiveCell.Value <> "" Then
         'strShName = ActiveCell.Value
         strShName = c.Value
         Set wsNew = Worksheets.Add
     
         wsNew.Name = strShName
    End If
    Sheets("ReadMe").Select
Next

 End Sub

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40399466
Thanks ProfessorJimJam.  I like mine better that will create based on a selection or list.  We are just trying to figure out how to address errors that can occur when a dup, bad char, or too many chars are used to create the sheet.
0
 
LVL 25

Accepted Solution

by:
ProfessorJimJam earned 250 total points
ID: 40399753
RWayneH,

here you go your code.  takes care of everything you have asked.

Sub AddSheet()
 
   Dim wsNew As Worksheet
   Dim strShName As String
   Dim strSheetName As String, wks As Worksheet, bln As Boolean

    For Each c In Selection
    If IsEmpty(c.Value) Then GoTo SkipToHere:

    

     If Len(c.Value) > 31 Then GoTo SkipToHere:
    
    
    Dim IllegalCharacter(1 To 7) As String, i As Integer
    IllegalCharacter(1) = "/"
    IllegalCharacter(2) = "\"
    IllegalCharacter(3) = "["
    IllegalCharacter(4) = "]"
    IllegalCharacter(5) = "*"
    IllegalCharacter(6) = "?"
    IllegalCharacter(7) = ":"
    For i = 1 To 7
        If InStr(c.Value, (IllegalCharacter(i))) > 0 Then GoTo SkipToHere:
     Next i

strSheetName = Trim(c.Value)
    On Error Resume Next
    Set wks = ActiveWorkbook.Worksheets(strSheetName)
If Err.Number = 0 Then GoTo SkipToHere:
   
       
    If c.Value <> "" And Not InStr(c.Value, (IllegalCharacter(i))) > 0 Then

         strShName = c.Value
         Set wsNew = Worksheets.Add

         wsNew.Name = strShName
    End If
       'Sheets("ReadMe").Select
SkipToHere:
Next
End Sub

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40401950
One small issue maybe that if one in the selection is skipped and not created... there is no way of knowing.  Perhaps we could have the cell skipped get a background color?  This would at least highlight the issue.  Hope it is not too late to add that?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40409856
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
 

Author Closing Comment

by:RWayneH
ID: 40410223
Thanks for the help.  EXCELent!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now