简体   繁体   中英

excel VBA worksheet_activate method not working correctly

I have a spreadsheet with a small Subroutine in it that should do three things when the tab for the sheet "Template" is clicked: 1. make a copy of the "Template" sheet and place it before the original "Template" sheet 2. change the name of the copied sheet to be today's date (10-13-2016) 3. change the contents of cell B1 to be today's date (Thursday, Oct 13, 2016)

The code listed below does these things sort of. The two things I need help on is this: 1. to get the sheet to copy I have to click another sheet and then click back on the "Template" sheet. I'd like to be able to just click the "Template" tab and have it create the copy, even if the "Template" sheet is already the active sheet. 2. for some reason the VBA code prevents me from deleting the tab that is created when you click the "Template" tab.

Private Sub Worksheet_Activate()

Application.EnableEvents = False

If ActiveSheet.Name = "Template" Then

    Worksheets("Template").Copy before:=Worksheets("Template")

    ActiveSheet.Range("B2").Select

    ActiveCell.FormulaR1C1 = Format(Date, "dddd, mmm d, yyyy")

    ActiveSheet.Name = Format(Date, "mm-dd-yyyy")

End If

Application.EnableEvents = True

End Sub

I know this is probably very simple but I haven't been able to find any reference to this behavior anywhere. Any and all help will be much appreciated.

you wouldn't use Worksheet_Activate() because it would be copied along with the worksheets copies, thus having copied worksheets generate other worksheets

so you want to use Workbook_SheetActivate() event handler

even then, you must be aware that upon deleting a sheet just preceeding "Template", the active sheet becomes "Template" (iethe next one) thus activating the cloning procedure and making it seem as if "VBA code prevents" you "from deleting the tab"

then type this code in ThisWorkBook code pane:

Option Explicit

Dim nextShtName As String

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim newName As String

    If nextShtName = "Template" Then
        nextShtName = ""
    Else
        If Sh.Name = "Template" Then
            newName = Format(Date, "mm-dd-yyyy")
            If GetSheet(newName) Is Nothing Then
                Application.EnableEvents = False
                On Error GoTo exitsub

                Sh.Copy before:=Worksheets("Template")
                With ActiveSheet
                    .Range("B2").FormulaR1C1 = Format(Date, "dddd, mmm d, yyyy")

                    .Name = newName
                End With
exitsub:
                Application.EnableEvents = True
            Else
                MsgBox "sheet '" & newName & "' already in this workbook", vbInformation
            End If
        End If
    End If

End Sub

Function GetSheet(shtName As String) As Worksheet
    On Error Resume Next
    Set GetSheet = Worksheets(shtName)
End Function

Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
    Dim i As Long
    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = Sh.Name Then Exit For
    Next i
    nextShtName = Worksheets(i + 1).Name
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM