VBA to copy template sheet and rename it along with if name deleted corresponding sheet will be deleted.

BSHANEH

New Member
Joined
Jul 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Can someone help me out? I know very little about VBA code. I'm currently working on a diver profile log that I would like to be able to enter the name of the person on the master sheet and it copy and rename the template sheet to that name as well as entering that name in cell A1 in the copy of the template.I would like it to put a message up if I enter a duplicate name. If I delete that name, I would like to have it delete the corresponding sheet as well. If possible, I would also like to be able to click on any name on the master sheet and it bring up that sheet. I've tried a few different code's and none seem to work like I want. I appreciate any help.
 

Attachments

  • Master.JPG
    Master.JPG
    121.5 KB · Views: 4
  • Template.JPG
    Template.JPG
    204.1 KB · Views: 4

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Assume there are at least two sheets at begginning: "Master" and "Template"
Try below code. it must be placed in worksheet "Master" module (right click on tab "Master"/view code)
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Dim lr&, i&, c&, cell As Range, nameS(1 To 100000, 1 To 2)
Dim ws As Worksheet, temp As Worksheet
If Target.Column = 1 And Target.Row > 2 Then
lr = Cells(Rows.Count, "A").End(xlUp).Row
Set temp = Worksheets("Template")
For Each cell In Target
    If Not IsEmpty(cell) And WorksheetFunction.CountIf(Range("A3:A" & lr), cell) > 1 Then
        MsgBox "Dupplicate name. Try again"
        Target.ClearContents
        Exit Sub
    End If
Next
    For Each ws In Sheets
        If ws.name <> "Master" And ws.name <> "Template" Then
            If lr = 2 Then
                ws.Delete
            Else
                i = i + 1
                nameS(i, 1) = ws.name
            End If
        End If
    Next
    For Each cell In Range("A3:A" & lr)
        If lr = 2 Then Exit Sub
        If Not IsEmpty(cell) Then
            For Each ws In Sheets
                If ws.name <> "Master" And ws.name <> "Template" And ws.name = cell.Value Then GoTo z:
            Next
            temp.Copy after:=Sheets(Sheets.Count)
            ActiveSheet.name = cell.Value
            ActiveSheet.Range("A1").Value = cell.Value
            Worksheets("Master").Activate
        End If
z:
    Next
    For Each ws In Sheets
        If ws.name <> "Master" And ws.name <> "Template" And _
        WorksheetFunction.CountIf(Range("A3:A" & lr), ws.name) = 0 Then ws.Delete
    Next
End If
Application.ScreenUpdating = True
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If IsEmpty(Target) Or Target.Count > 1 Or Target.Row < 3 Or Target.Column <> 1 Then Exit Sub
Worksheets(Target.Value).Activate
End Sub
 
Upvote 0
Solution
This worked perfectly can't thank you enough for the help.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top