Macro to name the newly created worksheets

Krisz06

New Member
Joined
May 25, 2017
Messages
26
Hi guys,
I'm trying to create a macro which activate when I'm clicking on add new worksheet.

What I'm trying is that an inputbox appear, there I can add the name I want.
But if the name is already exist, an other inputbox appear till I don't put a value which can be the name.




Thank you!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi guys,
I'm trying to create a macro which activate when I'm clicking on add new worksheet.

What I'm trying is that an inputbox appear, there I can add the name I want.
But if the name is already exist, an other inputbox appear till I don't put a value which can be the name.




Thank you!

Put this bit in the thisworkbook part:

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)

Dim strSheetName As String

line1:

    strSheetName = InputBox("Enter New Sheet Name", "Add Sheet")
    
    If SheetExists(strSheetName) = False Then
        If strSheetName = "" Then
            MsgBox "Sheet Not Named", vbInformation
        Else
            ActiveSheet.Name = strSheetName
        End If
    Else
        MsgBox "Sheet Name Exists try again", vbCritical
        GoTo line1
    End If

End Sub

and this bit in a module

Code:
Function SheetExists(strSheetName As String) As Boolean

Dim ws As Worksheet

    SheetExists = False

    For Each ws In Worksheets
        If ws.Name = strSheetName Then
            SheetExists = True
        End If
    Next

End Function

Hope this helps
 
Upvote 0
Put this bit in the thisworkbook part:

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)

Dim strSheetName As String

line1:

    strSheetName = InputBox("Enter New Sheet Name", "Add Sheet")
    
    If SheetExists(strSheetName) = False Then
        If strSheetName = "" Then
            MsgBox "Sheet Not Named", vbInformation
        Else
            ActiveSheet.Name = strSheetName
        End If
    Else
        MsgBox "Sheet Name Exists try again", vbCritical
        GoTo line1
    End If

End Sub

Hope this helps


It works perfectly.
The module one I had the same, but the first part I tried in different ways which were not working..

this "line1" thing seems good, but can you please explain the logic of it? In just simple google search I couldn't find anything about it.
It works kind of like a bookmark and I can just use it with GoTo line1 commands in any kind of codes?


Thank you for your answer!!!
 
Upvote 0
Glad I could help.

I'm not sure if using Goto is the most elegant way of doing it but as long as the result is right.

GoTo is used to jump to different parts of your code. You can replace Line1 with any label for example

Code:
Sub MyTest()

    If ActiveCell = 10 Then
        GoTo Hell
    End If

    ' Some other code could go here which would be skipped

Hell:

    MsgBox "Done"

End Sub

the code for your answer starts top to bottom as normal but when the condition isn't met in the input box then the goto line starts the code again like a loop.

Hope this makes sense
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,806
Members
449,337
Latest member
BBV123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top