Reputation: 93
I have been looking into using VBA to write VBA Code. So that ultimately I can write an algorithm to write loops depending on a variable entered.
I have come across the below, which looks as if it will add code to the VBA project but I am missing something for it to work. Struggling to find anything on this. I know that 'ThisWorkbook' is the issue. Any ideas to what I need to call it to be able to reference that sheet (same name) in the VBA project.
Thanks
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
Upvotes: 2
Views: 4997
Reputation: 93
Thanks, That has done it.
For reference the missing code is:
VBComponents(ThisWorkbook.CodeName).CodeModule
Upvotes: 1