Wim Geuens
Member
Hello,
I found a vba code that allows me to add new sheets tot a workbook.
The only thing that it does not do is keeping the page format (print area). The original sheet is printed on 2 pages, but when I run the macro it print on more then 2 pages because it doesn't hold on to the print area.
Can someone help me to adjust the code?
thanks!
I found a vba code that allows me to add new sheets tot a workbook.
The only thing that it does not do is keeping the page format (print area). The original sheet is printed on 2 pages, but when I run the macro it print on more then 2 pages because it doesn't hold on to the print area.
Can someone help me to adjust the code?
thanks!
Code:
Sub AddSheetz()
'First, jump through the validation hoops (need Variant to error-check)
Dim AddSheetQuestion As Variant
'Define the application input box question
showAddSheetQuestion:
AddSheetQuestion = Application.InputBox _
("Please enter the name of the sheet you want to add," & vbCrLf & _
"or click the Cancel button to cancel the addition:", _
"What sheet do you want to add?")
'Cancel or the X was clicked
If AddSheetQuestion = False Then
MsgBox "You clicked the Cancel button." & vbCrLf & _
"No new sheet will be added.", 64, "Cancel was clicked."
Exit Sub
'OK was clicked without anything being entered
ElseIf AddSheetQuestion = "" Then
MsgBox "You clicked OK but entered nothing." & vbCrLf & vbCrLf & _
"Please type in a valid sheet name." & vbCrLf & _
"Otherwise, you must click Cancel to exit." & vbCrLf & vbCrLf & _
"Click OK and let's try again.", 48, "Hmmm...that didn't make sense..."
GoTo showAddSheetQuestion
End If
'See if a worksheet exists that is named as the new name being attempted to add.
If SheetExists(CStr(AddSheetQuestion)) Then
MsgBox "A worksheet already exists that is named " & AddSheetQuestion & "." _
& vbCrLf & vbCrLf & _
"Please click OK, verify the name you really" & vbCrLf & _
"want to add, and try again." & vbCrLf & vbCrLf & "Sheet addition cancelled.", _
48, "Sorry, that name already taken."
GoTo showAddSheetQuestion
End If
'Error trap for naming syntax error
On Error GoTo ErrorHandler1
'Here's the actual sheet addition code
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
'Add and name the new sheet
Worksheets.Add
With ActiveSheet
.Name = AddSheetQuestion
.Move After:=Worksheets(Worksheets.Count)
End With
'Make the Template sheet visible, and copy it
Worksheets("Sheet1").Cells.Copy
'Re-activate the new worksheet, and paste
'Worksheets(AddSheetQuestion).PasteSpecial
With Worksheets("AddSheetQuestion")
.Cells.PasteSpecial xlValues
.Cells.PasteSpecial xlFormats
End With
With Application
.CutCopyMode = False
.GoTo Range("A1"), True
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
'Inform the user the macro is completed
MsgBox "The new sheet name ''" & AddSheetQuestion & "'' has been added.", _
64, "Sheet addition successful."
Exit Sub
'If a sheet naming syntax occurs:
ErrorHandler1:
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
MsgBox "You entered a character that cannot be part of a sheet name." & vbNewLine & _
"Sheet names cannot contain the following:-" & vbNewLine & _
"'':'' , ''/'' , ''\'' , ''?'' , ''*'' , ''['' , or '']''.", _
16, "Name syntax error."
On Error GoTo 0
GoTo showAddSheetQuestion
End Sub
Function SheetExists(strWSName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Worksheets(strWSName)
If Not ws Is Nothing Then SheetExists = True
'Boolean function assumed to be False unless set to True
End Function