I have two userforms in my Excel VBA project.
In form 1 I have a function in which I collect three public variables after which I open the second form and pass the variables.
I would like the first form to close when the second one opens. I tried different set ups with modal and modeless opening of both forms and putting the Unload statement in the Initialize of the second form as well as at the end of the sub in the first form.
This is what I have now.
In my first form (FormStart):
Private Sub Btn_RondeHeropenen_Fx_Click()
'Here I collect my variables to populate the ScanTool form
VerwerkenRetour = True 'this is a public variable that I want to use to determine where I came from when opening ScanTool
ScanTool.Show
End Sub
In the ScanTool form:
Private Sub UserForm_Initialize()
If VerwerkenRetour = True Then
'I first write my variables into the form (works fine)
Btn_Start_Fx_Click 'and then I run a function (works fine as well)
Unload (FormStart)
End If
End Sub
The above code does does not close the FormStart. In this code version I get:
"Runtime Error 361: Can not unload an object or remove it from memory"
I have moved the Unload statement to the end of the sub in the FormStart, immediately below the Show.ScanTool instruction, but there it is probably disregarded as it is no longer the active form.
Just to show how it can work, the principle Two userforms one with a button
UserForm1 Code:
Option Explicit
Private Sub CommandButton1_Click()
Me.Hide ' hides the UF
UserForm2.Show vbModal ' loads and show the other UF
Unload Me ' When UserForm1 is closed it returns here and we close it
End Sub