Macro to create a new sheet as per Cell values typed in a range

prkhan56

New Member
Joined
Feb 5, 2003
Messages
23
Dear All,

I am using Excel 2013 and I would like to have a macro which should create a new sheet when a value is typed in a cell and entered in the range eg A2:A100

If Sales is typed in Cell A2 a new sheet should be created in the workbook with the name Sales, if Expenses is typed in Cell A3 then a new sheet should be created after Sales sheet as Expenses so on and so forth. All new sheets will always be created after the last sheet in the workbook.

If the sheet is existing with the name typed in the Range A2:A100 then the macro should display a message box that sheet already exists!

Thanks in advance

Rashid Khan
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,

Add this macro to the sheet you want to run it in.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range


    Set KeyCells = Range("A2:A100")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Target.Value
    End With
    
    End If
End Sub
 
Upvote 0
Another option
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Intersect(Target, Range("A2:A100")) Is Nothing Or Target.Value = "" Then Exit Sub
   If Evaluate("isref('" & Target.Value & "'!A1)") Then
      MsgBox "Sheet " & Target.Value & " already exists"
   Else
      Sheets.Add(, Sheets(Sheets.Count)).Name = Target.Value
   End If
End Sub
 
Upvote 0
Hello there
Your code does not give a message box if there is a duplicate name
it highlights the following line with run time error

.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Target.Value

Thanks for your time
 
Upvote 0
Dear Fluff
Thanks for the code.
It works as per my requirement but a small problem if you could fix please

Now when the new sheet is created the cell pointer is in the newly create sheet.

Is it possible to remain in the Active sheet when the new sheet is created.

Thanks for your time and help
Rashid Khan
 
Upvote 0
Simply add
Code:
End If
[COLOR=#ff0000]Me.Activate[/COLOR]
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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