dannyboy1982
Board Regular
- Joined
- Mar 6, 2015
- Messages
- 60
Hi,
I need to write some VBA code so that when I click a command button, it opens up a file (called IBSXL.xlsm) and automatically writes some code in the 'Thisworkbook' module. I replace this file everyday, so I cannot leave the code in the module as it gets overwritten each day. Is there a way to do this in VBA? I have managed to open the file and transfer a 'normal' module but cannot work out how to get the code into the 'Thisworkbook' module.
I need the code in the 'Thisworkbook' module as I want the code to run as soon as the workbook opens without interaction from the user.
Any help would be greatly appreciated.
I need to write some VBA code so that when I click a command button, it opens up a file (called IBSXL.xlsm) and automatically writes some code in the 'Thisworkbook' module. I replace this file everyday, so I cannot leave the code in the module as it gets overwritten each day. Is there a way to do this in VBA? I have managed to open the file and transfer a 'normal' module but cannot work out how to get the code into the 'Thisworkbook' module.
Code:
Public Sub TransferModule()
Const MODULE_NAME As String = "module16" ' Name of the module to transfer
Const TEMPFILE As String = "c:\users\name\desktop\module16.bas" ' temp textfile
Dim WBK As Workbook
On Error Resume Next
'**Create new workbook
Set WBK = Workbooks.Open("C:\users\name\desktop\IBSXL.xlsm")
'** export the module to a textfile
ThisWorkbook.VBProject.VBComponents(MODULE_NAME).Export TEMPFILE
'import the module to the new workbook
WBK.VBProject.VBComponents.Import TEMPFILE
'kill the textfile
Kill TEMPFILE
End Sub
I need the code in the 'Thisworkbook' module as I want the code to run as soon as the workbook opens without interaction from the user.
Any help would be greatly appreciated.