Roderick_E
Well-known Member
- Joined
- Oct 13, 2007
- Messages
- 2,051
Ok so I'm working to launch a subroutine from one workbook (Test2), then have it open another workbook (Test1) and replace a module and userform on Test2 with a module and userform from Test1.
I got the module part to work but I'm having trouble with the Userform. Probably because of where and when I'm trying to unload the form; since I have to use form I want to replace to actually launch the subroutine.
Here's the code; a function and a subroutine with issue lines commented out.
Launched from TEST2
I think if I can unload/close Userform1 within the function it may work.
I got the module part to work but I'm having trouble with the Userform. Probably because of where and when I'm trying to unload the form; since I have to use form I want to replace to actually launch the subroutine.
Here's the code; a function and a subroutine with issue lines commented out.
Launched from TEST2
Code:
Private Sub CommandButton1_Click()
Dim swbk As Workbook
Dim twbk As Workbook
Dim modname As String
Dim strfiletoopen As String
strfiletoopen = Application.GetOpenFilename _
(Title:="Please choose a file to open", _
FileFilter:="Excel Files *.xls* (*.xls*),")
Set swbk = Workbooks.Open(strfiletoopen)
modname = "Module1"
Set twbk = ThisWorkbook
updatexlst swbk, modname, twbk
Unload Me '[COLOR="#FF0000"]need to remove and put later perhaps in function[/COLOR]
End Sub
Code:
Function updatexlst(sourcewb As Workbook, modname As String, targetwb As Workbook)
Dim swbk As Workbook
Dim tempForm As UserForm
strFolder = sourcewb.Path
If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & "\"
strTempFile = strFolder & "~tmpexport.bas"
strTempForm = strFolder & "~tmpexport.frm"
On Error Resume Next
Application.DisplayAlerts = False
targetwb.VBProject.VBComponents(modname).Name = "oldmod"
'*** unload TEST2 userform here somehow?? ***
[COLOR="#FF0000"] 'targetwb.VBProject.VBComponents("Userform1").Name = "oldform"[/COLOR]
sourcewb.VBProject.VBComponents(modname).Export strTempFile
[COLOR="#FF0000"] 'sourcewb.VBProject.VBComponents("Userform1").Export strTempForm[/COLOR]
targetwb.VBProject.VBComponents.Import strTempFile
[COLOR="#FF0000"] 'targetwb.VBProject.VBComponents.Import strTempForm[/COLOR]
targetwb.VBProject.VBComponents.Remove targetwb.VBProject.VBComponents("oldmod")
sourcewb.Close
Application.DisplayAlerts = True
Kill strTempFile
Kill strTempForm
On Error GoTo 0
End Function
I think if I can unload/close Userform1 within the function it may work.