改成下面这样就行了。
ShtCodeName = ThisWorkbook.Worksheets("AAAA").CodeName
With ThisWorkbook.VBProject.VBComponents.Item(ShtCodeName).CodeModule
.InsertLines 1, "Private Sub Worksheet_Change(ByVal Target As Range)"
.InsertLines 2, "msgbox ""生成事件成功"""
.InsertLines 3, "'这是一个注释示例"
.InsertLines 5, "End Sub"
End With
试试吧。
换种思路行不?
Private Sub Workbook_Open()
For i = 1 To Worksheets.Count
If Sheets(i).Name = "AAAA" Then Sheets(i).Cells.Delete: Exit Sub
Next
Worksheets.Add after:=Worksheets(Sheets.Count)
Worksheets(Sheets.Count).Name = "AAAA"
End Sub
= Val(Target) * 3 End If End Sub 说明:只要把Worksheet_Change 改为 Worksheet_SelectionChange 即可 1楼,正确,学习了