sparky2205
Well-known Member
- Joined
- Feb 6, 2013
- Messages
- 507
- Office Version
- 365
- 2016
- Platform
- Windows
Hi folks,
I have a macro that allows users to delete certain worksheets from protected workbooks.
The user is not allowed delete any worksheet that exists in the template.
However, there is a macro to allow the user to add a new sheet and rename it whatever they want.
They are allowed to delete these newly created worksheets.
I wanted to make this macro generic so it could be easily transferred between workbooks.
So the setup is as follows:
Create a worksheet called "WS_Names" in the workbook and list all the existing (can't be deleted) worksheet names in cell A1 as follows;
front sheet|page tracker|equipment|assembly - npi (optional)|assembly - production 100%|assembly - production sample|boy1 or boy2 setup|final inspection|sterilization & release|ws_names|
Then the code:
Everything works fine for:
• Deleting an existing worksheet (not allowed)
• Deleting a newly created worksheet that doesn't contain any part of the name of an existing worksheet
• Deleting a newly created worksheet that has the first part of an existing worksheet name as its name e.g. existing worksheet = page tracker; new worksheet = page
The problem:
• Deleting a newly created worksheet that has the last part of an existing worksheet name as its name e.g. existing worksheet = page tracker; new worksheet = tracker
In my code I get the starting position of the worksheet name to be deleted in the string and return the existing worksheet name from that point.
But if the worksheet to be deleted is called tracker and the existing worksheet name is page tracker then tracker will be returned in both cases and my test fails.
What I'm really looking for here is a way to return the complete name from the string given a part of the name, in particular the last part of the name.
Please let me know if you require any clarifications or further information.
Thanks folks.
I have a macro that allows users to delete certain worksheets from protected workbooks.
The user is not allowed delete any worksheet that exists in the template.
However, there is a macro to allow the user to add a new sheet and rename it whatever they want.
They are allowed to delete these newly created worksheets.
I wanted to make this macro generic so it could be easily transferred between workbooks.
So the setup is as follows:
Create a worksheet called "WS_Names" in the workbook and list all the existing (can't be deleted) worksheet names in cell A1 as follows;
front sheet|page tracker|equipment|assembly - npi (optional)|assembly - production 100%|assembly - production sample|boy1 or boy2 setup|final inspection|sterilization & release|ws_names|
Then the code:
Code:
Sub Delete_Worksheet()
Dim sWSName As String
Dim icount As Integer
Dim sWSNames As String
Dim ws As Worksheet
Dim icountname As Integer
Dim ans As String
Dim pw As String
Dim sfind As String
pw = "Test"
' Prevent screen flicker when executing
Application.ScreenUpdating = False
icountname = 0
' Get the list of Worksheet names that are not allowed to be deleted
sWSNames = Worksheets("WS_Names").Cells(1, "A").Value
' Ask the user for the name of the Worksheet to be deleted
sWSName = Application.InputBox("Enter the Worksheet name to delete", Type:=2)
sfind = InStr(sWSNames, LCase$(sWSName))
' Check if the user presses Cancel or doesn't make an entry in the inputbox. Exit if either is true.
If sWSName = "False" Or sWSName = "" Then
Exit Sub
End If
' Check if the entered Worksheet name exists in the spreadsheet
For Each ws In ThisWorkbook.Worksheets
If UCase(ws.Name) = UCase(sWSName) Then
icountname = icountname + 1
End If
Next ws
If icountname = 0 Then
MsgBox "Worksheet name " & Chr(34) & sWSName & Chr(34) & " does not exist in this file"
Exit Sub
End If
' Ensure that the Worksheet name entered is not on the list of worksheets not be be deleted
If InStr(sWSNames, LCase$(sWSName)) = 0 Then
' If it is confirm with the user that they want to delete that worksheet
ans = MsgBox("You are deleting Worksheet " & Chr(34) & Application.WorksheetFunction.Proper(sWSName) & Chr(34) & ". Do you wish to proceed?", vbOKCancel)
' If the user changes their mind then cancel
If ans = vbCancel Then
Exit Sub
End If
' Otherwise delete the Worksheet
Sheets(sWSName).Select
' Prevent display of Excels basic alert message. See customised message above.
Application.DisplayAlerts = False
' Unprotect the Workbook to delete the Worksheet
ThisWorkbook.Unprotect Password:=pw
ActiveWindow.SelectedSheets.Delete
' Protect the Workbook again after the Worksheet is deleted
ThisWorkbook.Protect Password:=pw, structure:=True
' Reenable display of alert messages
Application.DisplayAlerts = True
' Check that the Worksheet name entered is on the list of worksheets not be be deleted...
ElseIf InStr(sWSNames, LCase$(sWSName)) <> 0 Then
' ...but only partially (e.g. not allowed to delete "Page Tracker" but allowed to delete newly added worksheet "Page".
If Mid(sWSNames, sfind, InStr(sfind, sWSNames, "|") - sfind) <> LCase(sWSName) Then
' If it is confirm with the user that they want to delete that worksheet
ans = MsgBox("You are deleting Worksheet " & Chr(34) & Application.WorksheetFunction.Proper(sWSName) & Chr(34) & ". Do you wish to proceed?", vbOKCancel)
' If the user changes their mind then cancel
If ans = vbCancel Then
Exit Sub
End If
' Otherwise delete the Worksheet
Sheets(sWSName).Select
' Prevent display of Excels basic alert message. See customised message above.
Application.DisplayAlerts = False
' Unprotect the Workbook to delete the Worksheet
ThisWorkbook.Unprotect Password:=pw
ActiveWindow.SelectedSheets.Delete
' Protect the Workbook again after the Worksheet is deleted
ThisWorkbook.Protect Password:=pw, structure:=True
' Reenable display of alert messages
Application.DisplayAlerts = True
Else:
MsgBox ("Deleting Worksheet " & Chr(34) & Application.WorksheetFunction.Proper(sWSName) & Chr(34) & " is not allowed")
End If
End If
' Reenable ScreenUpdating
Application.ScreenUpdating = True
End Sub
• Deleting an existing worksheet (not allowed)
• Deleting a newly created worksheet that doesn't contain any part of the name of an existing worksheet
• Deleting a newly created worksheet that has the first part of an existing worksheet name as its name e.g. existing worksheet = page tracker; new worksheet = page
The problem:
• Deleting a newly created worksheet that has the last part of an existing worksheet name as its name e.g. existing worksheet = page tracker; new worksheet = tracker
In my code I get the starting position of the worksheet name to be deleted in the string and return the existing worksheet name from that point.
But if the worksheet to be deleted is called tracker and the existing worksheet name is page tracker then tracker will be returned in both cases and my test fails.
What I'm really looking for here is a way to return the complete name from the string given a part of the name, in particular the last part of the name.
Please let me know if you require any clarifications or further information.
Thanks folks.