Excel VBA 如何向新添加的sheet中添加worksheet_change事件呢?

2025-01-05 04:40:52
推荐回答(3个)
回答1:

改成下面这样就行了。

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

试试吧。

回答2:

换种思路行不?

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

回答3:

= Val(Target) * 3 End If End Sub 说明:只要把Worksheet_Change 改为 Worksheet_SelectionChange 即可 1楼,正确,学习了