Write a VBA Code with another VBA Code into ThisWorkbook

Layla78

New Member
Joined
Jun 22, 2012
Messages
2
I need to write a code that writes another code but not into a module but rather into ThisWorkbook so that the code is executed on open, Private Sub Workbook_Open(). I have a code that writes a code into a module:
Dim VBP As VBProject
Dim VBC As VBComponent
Dim VBMod As CodeModule

Set VBP = ActiveWorkbook.VBProject
Set VBC = VBP.VBComponents.Add(vbext_ct_StdModule)

VBC.Name = "resultsPrint"

Set VBMod = VBC.CodeModule

....... lines to be written

End Sub

So this code writes the code (...lines to be written) into a module. I am assuming I just need to change VBC.CodeModule into something that is not a module but I don't know what that is. Hope this makes sense...

Thanks!
Layla
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try...

Code:
Dim VBP As VBProject
Dim VBC As VBComponent


Set VBP = ActiveWorkbook.VBProject


Set VBC = VBP.VBComponents("ThisWorkbook")


With VBC.CodeModule
    
    'lines to be inserted
    
End With
 
Upvote 0
Try...

Code:
Dim VBP As VBProject
Dim VBC As VBComponent


Set VBP = ActiveWorkbook.VBProject


Set VBC = VBP.VBComponents("ThisWorkbook")


With VBC.CodeModule
    
    'lines to be inserted
    
End With

Thank you. That is exactly what I ended up figuring out and it worked, so it's solved.
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,565
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top