Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Sub CreateNewSheetWithEvent()
Dim Wks As Worksheet
Dim SheetCodename As String
Dim SheetCodeModule As CodeModule
Dim CodeLine As Integer
Set Wks = ThisWorkbook.Worksheets.Add
SheetCodename = Wks.CodeName
Set SheetCodeModule = ThisWorkbook.VBProject.VBComponents(SheetCodename).CodeModule
With SheetCodeModule
CodeLine = .CountOfLines + 1
.InsertLines CodeLine, "Private Sub Worksheet_Activate()"
CodeLine = CodeLine + 1
.InsertLines CodeLine, EventCode_Line1
' ...
CodeLine = CodeLine + 1
.InsertLines CodeLine, EventCode_LineN
CodeLine = CodeLine + 1
.InsertLines CodeLine, "End Sub"
End With
Set SheetCodeModule = Nothing
Set Wks = Nothing
End Sub
Sub Newsheet()
Dim Wks As Worksheet
Dim SheetCodeModule As CodeModule
Dim CodeLine As Integer
Dim counter As Integer
[COLOR=green]' If Test sheet exists, delete[/color]
If ExistingSheet("Test") = True Then
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("Test").Delete
End If
Set Wks = ThisWorkbook.Worksheets.Add
Wks.Name = "Test"
On Error Resume Next
counter = 0
Do
Err.Clear
Set SheetCodeModule = ThisWorkbook.VBProject.VBComponents(Wks.CodeName).CodeModule
counter = counter + 1
Loop Until (Not SheetCodeModule Is Nothing) Or counter = 10
If Err.Number <> 0 Then
[COLOR=green]'Message to the user here?[/color]
Exit Sub
End If
On Error GoTo 0 [COLOR=green]'or On Error GoTo YourErrorHandlerLabel[/color]
With SheetCodeModule
CodeLine = .CountOfLines + 1
.InsertLines CodeLine, "Private Sub Worksheet_Activate()"
CodeLine = CodeLine + 1
.InsertLines CodeLine, "msgbox time"
CodeLine = CodeLine + 1
.InsertLines CodeLine, "End Sub"
End With
Set SheetCodeModule = Nothing
Set Wks = Nothing
End Sub