动态地将代码添加到新的Excel工作表

时间:2022-09-15 21:29:19

I need some help Regarding VBA. In My code I add an excel sheet and rename it and add a validation list to it.I need to run some code on changing value in that validation list.And that must run only on change of that particular cell. If I am not clear please let me know.Please Help me solving this.

我需要一些关于VBA的帮助。在我的代码中,我添加了一个excel表并重命名它并向其添加一个验证列表。我需要在该验证列表中更改值时运行一些代码。并且必须仅在更改该特定单元格时运行。如果我不清楚请告诉我。请帮我解决这个问题。

2 个解决方案

#1


7  

Instead of trying to create the individual code for each new worksheet with the Visual Basic Extensibility (see this link for further reading), simply use the Workbook wide event Workbook_SheetChange (you need to place it in the ThisWorkbookmodule).

而不是尝试使用Visual Basic Extensibility为每个新工作表创建单独的代码(请参阅此链接以进一步阅读),只需使用Workbook wide事件Workbook_SheetChange(您需要将其放在ThisWorkbookmodule中)。

In this event code first check, if the worksheet which caused the event is one of the newly created worksheets. This can be done most easily, be checking the .Name of the worksheet.

在此事件代码中,首先检查导致事件的工作表是否是新创建的工作表之一。这可以通过检查工作表的.Name来轻松完成。

#2


0  

you can use SelectionChange Event and Change event or it is also possible to use the event Thisworkbook module. SheetChange or SheetSelectionChange.

您可以使用SelectionChange事件和更改事件,也可以使用事件Thisworkbook模块。 SheetChange或SheetSelectionChange。

#1


7  

Instead of trying to create the individual code for each new worksheet with the Visual Basic Extensibility (see this link for further reading), simply use the Workbook wide event Workbook_SheetChange (you need to place it in the ThisWorkbookmodule).

而不是尝试使用Visual Basic Extensibility为每个新工作表创建单独的代码(请参阅此链接以进一步阅读),只需使用Workbook wide事件Workbook_SheetChange(您需要将其放在ThisWorkbookmodule中)。

In this event code first check, if the worksheet which caused the event is one of the newly created worksheets. This can be done most easily, be checking the .Name of the worksheet.

在此事件代码中,首先检查导致事件的工作表是否是新创建的工作表之一。这可以通过检查工作表的.Name来轻松完成。

#2


0  

you can use SelectionChange Event and Change event or it is also possible to use the event Thisworkbook module. SheetChange or SheetSelectionChange.

您可以使用SelectionChange事件和更改事件,也可以使用事件Thisworkbook模块。 SheetChange或SheetSelectionChange。