[Excel VBA] 新建一个工作表并为其添加代码

Sub AddShtAndCodes() Dim NewSht As Worksheet, MyCodes As String Set NewSht = Worksheets.Add NewSht.Name = "test" MyCodes = "Private Sub Worksheet_Activate()" & vbCrLf & "Msgbox ""test""" & vbCrLf & "End sub" Application.VBE.ActiveVBProject.VBComponents(NewSht.CodeName).CodeModule.InsertLines 1, MyCodes End Sub

你可能感兴趣的:(Excel,VBA)