Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Button()
- Call SimpleSheetAdd
- MsgBox ("Hello World") & Chr(10) & "Hubert Nowinski"
- End Sub
- Function SheetCheck(WorkBookName As String, WorkSheetName As String) As Boolean
- Dim Workbk As Workbook
- Dim WorkSht As Worksheet
- 'returns if a worksheet name is an empty string
- If Len(WorkSheetName) = 0 Then
- SheetCheck = True
- Exit Function
- End If
- 'checking if a worksheet exists
- Set Workbk = Workbooks(WorkBookName)
- For Each WorkSht In Workbk.Sheets
- If WorkSht.Name = WorkSheetName Then
- SheetCheck = False
- MsgBox ("Error!! Name is used in another sheet")
- Exit Function
- End If
- Next WorkSht
- SheetCheck = True
- End Function
- Sub SimpleSheetAdd()
- Dim NewName As String
- NewName = InputBox("Enter worksheet name:", "Add new worksheet", "whatever")
- Dim NameNotExist As Boolean
- Dim BookName As String
- BookName = "Lab5 HN.xlsm"
- 'check if a worksheet "new worksheet" exists in "SomeDocument.xlsm" workbook
- NameNotExist = SheetCheck("Lab5b HN.xlsm", NewName)
- 'if a worksheet does not exist, create it
- If NameNotExist Then
- Workbooks("Lab5b HN.xlsm").Activate
- ActiveWorkbook.Sheets.Add
- ActiveSheet.Name = NewName
- End If
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment