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
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
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
'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