+ Reply to Thread
Results 1 to 8 of 8

Need Macro to Create New Worksheet Named After Currently Selected Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    01-03-2013
    Location
    Boston, MA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Question Need Macro to Create New Worksheet Named After Currently Selected Cell

    Hello all,

    I am new to the forum and entirely new to VBA. I have been working on a project and have managed to write a couple macros thus far, one that returns the current sheet name to a cell, and two that protect or unprotect all sheets in the workbook.

    I am stuck on this macro now, and have been looking everywhere with no avail. What I need is just a simple macro to take the currently selected cell and create a new worksheet with that name (it is a string). I also need to paste a template into that new worksheet from a pre-existing sheet.

    Any help would be greatly appreciated, this is my first post. I am using Excel 2010 by the way.

    -Brett

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    This should help you with part1. A lot more information is needed to assist you with your second question.

    Sub New_Sheet()
    Dim myName As String
    
    myName = Selection.Value
    
    If myName = "" Then
        MsgBox ("Your selected cell does not have a value.  Please choose a different cell.")
        Exit Sub
    End If
    
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = myName
    
    End Sub

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    Hi,

    Maybe something like

    Sub AddSheet()
        Dim stShName As String
        stShName = ActiveCell
        Sheets("Template").Copy after:=ActiveSheet
        ActiveSheet.Name = stShName
    End Sub
    and to
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    See if this will get you started. The sub calls function GoodSheetName to validate a good sheet name

    Sub createsheeet()
     Dim shName As String
     
     shName = GoodSheetName(ActiveCell.Value)
     If Not shName = vbNullString Then
        If Not Evaluate("=ISREF('" & shName & "'!A1)") Then
            With Sheets("Template").Copy(After:=Worksheets(Worksheets.Count))
                .Name = shName
            End With
        End If
     End If
    End Sub
    
    Private Function GoodSheetName(ByVal strName As String) As String
      
         Dim vaIllegal As Variant
         Dim i As Long
         
        'List unwanted characters
        vaIllegal = Array(".", "?", "!", "*", "/", "", "[", "]", "‘", Chr(34), "|", "<", ">", "\", ":")
         
        'Remove all illegals
        For i = LBound(vaIllegal) To UBound(vaIllegal)
             strName = Replace(strName, vaIllegal(i), "")
         Next i
         
        GoodSheetName = Left$(strName,31)
         
    End Function
    Last edited by mike7952; 01-03-2013 at 01:17 PM.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    01-03-2013
    Location
    Boston, MA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    @mike

    Your macro gets me the copy of Template, called Template (2), and a Run-time error '424': Object Required. When I run debug, I get .Name = shName highlighted.

  6. #6
    Registered User
    Join Date
    01-03-2013
    Location
    Boston, MA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    @stnky

    That macro worked great.

    As for the second part, I have a template sheet that I want copied into the new sheet after it is made. It could be either the entire contents of the sheet, or just the values and format of columns A through N. Does that help?

    -Brett

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    Try this then

    Sub createsheeet()
     Const shTemplate As String = "Template"
     Dim shName As String
     
     shName = GoodSheetName(ActiveCell.Value)
     If Not shName = vbNullString Then
        If Not Evaluate("=ISREF('" & shName & "'!A1)") Then
            Sheets(shTemplate).Copy After:=Worksheets(Worksheets.Count)
            ActiveSheet.Name = shName
        End If
     End If
    End Sub
    
    Private Function GoodSheetName(ByVal strName As String) As String
      
         Dim vaIllegal As Variant
         Dim i As Long
         
        'List unwanted characters
        vaIllegal = Array(".", "?", "!", "*", "/", "", "[", "]", "‘", Chr(34), "|", "<", ">", "\", ":")
         
        'Remove all illegals
        For i = LBound(vaIllegal) To UBound(vaIllegal)
             strName = Replace(strName, vaIllegal(i), "")
         Next i
         
        GoodSheetName = strName
         
    End Function

  8. #8
    Registered User
    Join Date
    01-03-2013
    Location
    Boston, MA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Need Macro to Create New Worksheet Named After Currently Selected Cell

    @mike

    Nailed it, Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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