如何使用VBA编写VBA代码

我一直在研究使用VBA编写VBA代码。 所以最终我可以编写一个algorithm来根据input的variables编写循环。

我遇到了下面,看起来好像它会添加代码到VBA项目,但我失去了一些工作。 挣扎着find任何东西。 我知道“ThisWorkbook”是问题。 任何想法,我需要调用它能够引用该表(同名)在VBA项目。

谢谢

Option Explicit Sub test() Dim WB_Write_Code_Test As Workbook Dim WS_Test As Worksheet Dim nextline As Long Set WB_Write_Code_Test = Workbooks.Open("Write Code Test") Set WS_Test = WB_Write_Code_Test.Sheets("Test") With ActiveWorkbook.VBProject. _ VBComponents(ThisWorkbook).CodeModule nextline = .CountOfLines + 1 .InsertLines nextline, WS_Test.Cells(1, 1).Value End With End Sub 

谢谢,这已经完成了。

作为参考缺less的代码是:

 VBComponents(ThisWorkbook.CodeName).CodeModule