Worksheet Activate when user selects sheet NOT when sheet is created from a template sheet
Sorry to be a pain however i'm using a spreadsheet that has the following bit of code in it. to look at a range and create new sheets based on the entries in that range.
PHP Code:
Sub Add_NameWS2() Dim myCell As Range Dim ValidName As Boolean Application.ScreenUpdating = False With Worksheets("Maintain") For Each myCell In .Range(("A1"), .Cells(.Rows.Count, 1).End(xlUp)).Cells ValidName = True Sheets("Template").Visible = xlSheetVisible Worksheets("template").Copy after:=Worksheets(Worksheets.Count) On Error GoTo NameAlreadyUsed ActiveSheet.Name = myCell.Value If ValidName = False Then Application.DisplayAlerts = False Worksheets(Worksheets.Count).Delete Application.DisplayAlerts = True End If Next myCell End With ' **************** MsgBox "" & vbCrLf & vbCrLf & vbCrLf & "Updated:" & vbCrLf & "New Timesheets created." & vbCrLf & " ", vbInformation, "Indiuns:" Sheets("Maintain").Select Range("A1:a17").Select Selection.ClearContents Range("E9").Select Application.ScreenUpdating = True ' **************** Exit Sub
NameAlreadyUsed: ValidName = False Resume Next End Sub
this works great to create a new sheet from a template and name it accordingly however the new sheet has the following code.
PHP Code:
Private Sub Worksheet_Activate() ' macro to ask if they accept the terms 'Sub UserInput() Application.ScreenUpdating = False
Dim iReply As Integer Me.Protect "test", , , , True If Range("ae12") = "M" Then Range("W:AA").EntireColumn.Hidden = False ElseIf Range("ae12") = "X" Then Range("W:AA").EntireColumn.Hidden = True End If iReply = MsgBox(Prompt:="I accept that entering false timesheets is an act of gross misconduct", _ Buttons:=vbYesNo, title:="ACCEPTANCE") If iReply = vbYes Then If Range("H1").Value > "0.00" Then Beep msg = "You have entries on this timesheet!" & vbCrLf & " " & vbCrLf & "Do you wish to reset your timesheet?" & vbCrLf & " " & vbCrLf & "CAUTION! Action CAN NOT be undone!" style = vbYesNo + vbQuestion + vbDefaultButton2 title = "Action required" response = MsgBox(msg, style, title) If response = vbNo Then GoTo Xit 'exit sub Me.Protect "test", , , , True 'change test to your password On Error Resume Next Range("D14:E44").Value = "" Range("G14:s44").Value = "" Range("B49:b56").Value = "" Range("D49:E56").Value = "" Range("g49:s56").Value = "" ActiveSheet.Range("D14").Select MsgBox "Your entries will be removed.", vbOKOnly, "Indiuns:"
Else Xit: ActiveSheet.Range("D14").Select Exit Sub End If Exit Sub
ElseIf iReply = vbNo Then MsgBox "File wil now close", vbOKOnly, "Indiuns:" ActiveWorkbook.Save Application.Quit Else 'They cancelled (VbCancel) Exit Sub End If Exit Sub End Sub
again this works great as it askes the user if they accept the conditions, checks to see if the sheet has data and asks if they want to clear it. What it also does is unhide rows W to AA if a condition in a cell is met. These need to be in as once the sheet is created it stays till the user leave. It's used every month once created.
What i'd like to know is using the Sub Add_NameWS2() macro creates a new sheet however it runs through the Private Sub Worksheet_Activate() macro. is there a was of not having it run the Private Sub Worksheet_Activate() macro when the sheet is created BUT have the macro run when the user acutually selects the sheet just created?
Sorry i'd load up an example but due to some of the data i'm not allowed without taking a lot out.
Bookmarks