Unprotect sheet, insert data, re-protect sheet

CSS2018

New Member
Joined
Feb 5, 2018
Messages
5
I have a macro that will create a new employee sheet, copied from a hidden sheet, and pulls in information based on the employee name and number they input.

My problem is it does not pull in their name (based on input box) on the newly created sheet. I need some help unlocking the sheet, pulling in the name of the employee (to A5), and re-locking it. See bold.

Rich (BB code):
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Dim MySheetName As String
    MySheetName = InputBox("Enter Employee Name:" & vbCrLf & "[Firstname Lastname]", "", "")
    If MySheetName = "" Then
        MsgBox "No sheet name was entered."
        Exit Sub
        
    Else
        On Error Resume Next
        Dim EmployeeNumber As Variant
        EmployeeNumber = InputBox("Please enter employee number.", "", "")
        
        Set ws = Sheets(MySheetName)
        If Err.Number <> 0 Then
            Sheets("COPYME").Visible = True
            Sheets("COPYME").Copy After:=Sheets("PROJECT SUMMARY")
            ActiveSheet.Name = MySheetName
            Sheet4.Unprotect Password:="PassworD"
            Range("A5").Value = MySheetName
            Sheet4.Protect Password:="PassworD"
            Sheets("COPYME").Visible = xlVeryHidden
        Else
            MsgBox "Worksheet " & MySheetName & " already exists."
            Exit Sub
            
        End If
           Worksheets("Project Summary").Activate
           Rows("11:11").Select
           Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
           Range("B11").Value = MySheetName
           Range("C11").Value = EmployeeNumber
           Range("D11").Formula = "=INDIRECT(""'""&B11&""'!P3"")"
           Range("E11").Formula = "=INDIRECT(""'""&B11&""'!Q3"")"
           Range("F11").Formula = "=INDIRECT(""'""&B11&""'!P42"")"
    
    
    Range("B11").Select
    ActiveWorkbook.Worksheets("Project Summary").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Project Summary").Sort.SortFields.Add Key:=Range( _
        "B11"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Project Summary").Sort
        .SetRange Range("B11:F999")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    End If
    
    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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