szubert

lab5b

Nov 18th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Button()
  2. Call SimpleSheetAdd
  3.     MsgBox ("Hello World") & Chr(10) & "Hubert Nowinski"
  4. End Sub
  5.  
  6.  
  7. Function SheetCheck(WorkBookName As String, WorkSheetName As String) As Boolean
  8. Dim Workbk As Workbook
  9. Dim WorkSht As Worksheet
  10. 'returns if a worksheet name is an empty string
  11. If Len(WorkSheetName) = 0 Then
  12.         SheetCheck = True
  13. Exit Function
  14. End If
  15. 'checking if a worksheet exists
  16. Set Workbk = Workbooks(WorkBookName)
  17. For Each WorkSht In Workbk.Sheets
  18. If WorkSht.Name = WorkSheetName Then
  19.             SheetCheck = False
  20.             MsgBox ("Error!! Name is used in another sheet")
  21. Exit Function
  22.  End If
  23. Next WorkSht
  24.     SheetCheck = True
  25. End Function
  26.  
  27. Sub SimpleSheetAdd()
  28. Dim NewName As String
  29. NewName = InputBox("Enter worksheet name:", "Add new worksheet", "whatever")
  30. Dim NameNotExist As Boolean
  31. Dim BookName As String
  32.     BookName = "Lab5 HN.xlsm"
  33. 'check if a worksheet "new worksheet" exists in "SomeDocument.xlsm" workbook
  34.    NameNotExist = SheetCheck("Lab5b HN.xlsm", NewName)
  35. 'if a worksheet does not exist, create it
  36. If NameNotExist Then
  37.         Workbooks("Lab5b HN.xlsm").Activate
  38.         ActiveWorkbook.Sheets.Add
  39.         ActiveSheet.Name = NewName
  40. End If
  41.  
  42. End Sub
Advertisement
Add Comment
Please, Sign In to add comment