Create new sheet

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I would like a macro to create a new sheet and give it the name from the text entered into a textbox in a user form and copy all the formats and formulas from sheet master (which is veryhidden) to the new sheet.

Regards Damian
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi,

I would like a macro to create a new sheet and give it the name from the text entered into a textbox in a user form and copy all the formats and formulas from sheet master (which is veryhidden) to the new sheet.

Regards Damian

if you have a sheet that is veryhidden

then you can cope with

recording a macro copying one sheet to another then change the names accordingly

look up inputbox in vbA help


Helping you feed yourself
 
Upvote 0
I have written the following code which works, is there any way to stop invalid characters being entered for the sheet name?

Code:
Sub NewSheet()
Dim WSName As String
Dim ws As Worksheet
 
    WSName = InputBox("Enter new sheet name", "New Sheet")
    If WSName <> "" Then
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name = WSName Then GoTo NameError
    Next ws
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = WSName
    End If
    Exit Sub
 
NameError:
 MsgBox ("This sheet name already exists, please enter another name"), vbInformation, "Sheet Name Error"
End Sub
 
Upvote 0
I have written the following code which works, is there any way to stop invalid characters being entered for the sheet name?

Code:
Sub NewSheet()
Dim WSName As String
Dim ws As Worksheet
 
    WSName = InputBox("Enter new sheet name", "New Sheet")
 
If WSName <> "" Then
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name = WSName Then GoTo NameError
    Next ws
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = WSName
    End If
    Exit Sub
 
NameError:
 MsgBox ("This sheet name already exists, please enter another name"), vbInformation, "Sheet Name Error"
End Sub


Edit


WSName = InputBox("Enter new sheet name", "New Sheet")
If WSName <> "" Then
For Each ws In ActiveWorkbook.Worksheets



to

WSName = InputBox("Enter new sheet name", "New Sheet")
WSName=ReplaceChars(WsName)
If WSName <> "" Then
For Each ws In ActiveWorkbook.Worksheets


call the following

Code:
'used to ensure valid Excel Sheet names
'
Function ReplaceChars(sstr As String) As String
Dim slen As Long
Dim soutput As String
Dim x As Long
Dim StringToReplace As String
 
'bad characters for Excel sheet names 1997-
StringToReplace = "\/?*[]!'."
slen = Len(StringToReplace)
For x = 1 To slen
    sstr = Replace(sstr, Mid$(StringToReplace, x, 1), "_")
Next x
 
 
ReplaceChars = sstr
End Function

This replaces invalid characters with an UnderScore
My personal choice
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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