ShawnSPS
Board Regular
- Joined
- Mar 15, 2023
- Messages
- 61
- Office Version
- 2003 or older
- Platform
- Windows
In my current workbook, a user may press “Add” what it does is copies a worksheet from a hidden worksheet called “ablank” renames the sheet to whatever the user enter as a New client name for example “ABC Company” and then copies that name and places it in Cell “A2” of that sheet. What it does next its copies “A2” and place into a data validation list called “zdata” that is hidden which it then “Znotes” then has the name created in its dropdown list.
Is there an alternative that I can use that does that same without using a data validation list and hiding it to create that drop function in znotes. Also with the Delete button on that workbook it would delete the worksheet but also remove the name from the “zdata” in return removes the name from the drop down list.
I have give you the two macro’s that drives it and my workbook file to understand what its doing in a working environment. Thank you.
Add button macro:
Public Sub CopySheetAndRenamePredefined()
Application.ScreenUpdating = False
Dim newName As String, i As Long
newName = InputBox("Enter New Client Name:")
If newName = "" Then
MsgBox ("User canceled!")
Exit Sub
End If
If Evaluate("isref('" & newName & "'!A1)") Then
MsgBox ("Sheet " & newName & " already exists." & Chr(10) & "Please enter a different name.")
Exit Sub
End If
With Sheets("Zdata")
.Unprotect Password:="Pila1DA.#"
.Cells(.Rows.Count, "A").End(xlUp).Offset(1) = newName
.Protect Password:="Pila1DA.#"
End With
Sheets("Ablank").Visible = True
Worksheets("Ablank").Copy After:=Worksheets(Sheets.Count)
With ActiveSheet
.Unprotect Password:="Pila1DA.#"
.Name = UCase(Trim(newName))
.Range("A2").Value = UCase(Trim(.Name))
.Protect Password:="Pila1DA.#"
End With
For i = 1 To Application.Sheets.Count
For j = 1 To Application.Sheets.Count - 1
If UCase$(Application.Sheets(j).Name) > UCase$(Application.Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
Next
Next
Sheets(newName).Activate
Sheets("Ablank").Visible = False
ActiveWorkbook.Save
Application.ScreenUpdating = True
MsgBox "The tabs have been sorted from A to Z."
End Sub
Delete button Macro
Sub check_sheet_delete()
Application.ScreenUpdating = False
Dim mySheet As String, fnd As Range
mySheet = InputBox("ENTER CLIENT NAME TO DELETE")
If mySheet = "" Then Exit Sub
If Evaluate("isref('" & mySheet & "'!A1)") Then
Application.DisplayAlerts = False
Sheets(mySheet).DELETE
Application.DisplayAlerts = True
With Sheets("Zdata")
.Unprotect Password:="Pila1DA.#"
Set fnd = .Range("A:A").Find(mySheet, LookIn:=xlValues, lookat:=xlWhole)
fnd.DELETE shift:=xlUp
.Protect Password:="Pila1DA.#"
End With
Else
MsgBox ("Sheet " & mySheet & " does not exist.")
End If
Application.ScreenUpdating = False
End Sub
Is there an alternative that I can use that does that same without using a data validation list and hiding it to create that drop function in znotes. Also with the Delete button on that workbook it would delete the worksheet but also remove the name from the “zdata” in return removes the name from the drop down list.
I have give you the two macro’s that drives it and my workbook file to understand what its doing in a working environment. Thank you.
Add button macro:
Public Sub CopySheetAndRenamePredefined()
Application.ScreenUpdating = False
Dim newName As String, i As Long
newName = InputBox("Enter New Client Name:")
If newName = "" Then
MsgBox ("User canceled!")
Exit Sub
End If
If Evaluate("isref('" & newName & "'!A1)") Then
MsgBox ("Sheet " & newName & " already exists." & Chr(10) & "Please enter a different name.")
Exit Sub
End If
With Sheets("Zdata")
.Unprotect Password:="Pila1DA.#"
.Cells(.Rows.Count, "A").End(xlUp).Offset(1) = newName
.Protect Password:="Pila1DA.#"
End With
Sheets("Ablank").Visible = True
Worksheets("Ablank").Copy After:=Worksheets(Sheets.Count)
With ActiveSheet
.Unprotect Password:="Pila1DA.#"
.Name = UCase(Trim(newName))
.Range("A2").Value = UCase(Trim(.Name))
.Protect Password:="Pila1DA.#"
End With
For i = 1 To Application.Sheets.Count
For j = 1 To Application.Sheets.Count - 1
If UCase$(Application.Sheets(j).Name) > UCase$(Application.Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
Next
Next
Sheets(newName).Activate
Sheets("Ablank").Visible = False
ActiveWorkbook.Save
Application.ScreenUpdating = True
MsgBox "The tabs have been sorted from A to Z."
End Sub
Delete button Macro
Sub check_sheet_delete()
Application.ScreenUpdating = False
Dim mySheet As String, fnd As Range
mySheet = InputBox("ENTER CLIENT NAME TO DELETE")
If mySheet = "" Then Exit Sub
If Evaluate("isref('" & mySheet & "'!A1)") Then
Application.DisplayAlerts = False
Sheets(mySheet).DELETE
Application.DisplayAlerts = True
With Sheets("Zdata")
.Unprotect Password:="Pila1DA.#"
Set fnd = .Range("A:A").Find(mySheet, LookIn:=xlValues, lookat:=xlWhole)
fnd.DELETE shift:=xlUp
.Protect Password:="Pila1DA.#"
End With
Else
MsgBox ("Sheet " & mySheet & " does not exist.")
End If
Application.ScreenUpdating = False
End Sub