I have developed a userform in EXCEL 2016 for PC. If the user launches the userform (from a button my add-in adds to the ribbon) from the same workbook/worksheet as the last launch or show, then I want the userform to remember all the users settings from the last use. Therefore, when the user is finished with a particular use, I simply hide the form. This works well so far.
HOWEVER, if the user shows the userform subsequently from a different worksheet or workbook, then EXCEL "snaps" back to the original workbook/worksheet the userform was launched from. In this case, I want the userform to reset and appear in the "new" active workbook/worksheet.
I can imagine pseudocode like this:
Public wb0 As Workbook
Public ws0 As Worksheet
' IF the userform is being used for the 1st time
Public Sub UserForm_Initialize()
yada yada yada...
wb0 = current workbook
ws0 = current worksheet
End Sub
Private Sub UserForm_Activate()
If wb0 <> ActiveWorkbook OR ws0 <> ActiveWorkSheet Then
(**Force the userform to open in the current workbook/worksheet**)
Call Reset
wb0 = current workbook
ws0 = current worksheet
End If
End Sub
However, I am not sure how to:
1) Test whether the current active workbook and worksheet are the same as the active workbook and worksheet from the last use. Should I set wb0 to the Name property of the ActiveWorkbook?
wb0 = ActiveWorkbook.Name
And then the test would be:
If wb0 <> ActiveWorkbook.Name ...
2) Also, not sure how to force the userform to open in the current worksheet.
====
Or, perhaps it would be best to have something like:
Private Sub UserForm_Activate()
If wb0 <> ActiveWorkbook OR ws0 <> ActiveWorkSheet Then
Unload userform
Reload userform
End If
End Sub
That way everything would be automatically reset...
====
Can someone please advise?
Thanks!!
Dan
I do this with some UserForms in a big add-in.
Imagine a UserForm named F_UserForm. The massively stripped-down module that calls it is shown below:
Option Explicit
Dim gF_UserForm As F_UserForm
Sub CallUserForm()
If Not gF_UserForm Is Nothing Then
On Error Resume Next
If gF_UserForm.ActiveWkbk.FullName <> ActiveWorkbook.FullName Then
Unload gF_UserForm
Set gF_UserForm = Nothing
End If
End If
If gF_UserForm Is Nothing Then
Set gF_UserForm = New F_UserForm
End If
With gF_UserForm
Set .ActiveWkbk = ActiveWorkbook
.Show
End With
End Sub
The stripped-down UserForm code is shown below:
Option Explicit
Dim m_Wkbk As Workbook
Public Property Set ActiveWkbk(wkbk As Workbook)
Set m_Wkbk = wkbk
End Property
For the OP's situation, my main routine can be replaced with the following, and the pieces I've added to the userform's code above need not change, but should be incorporated into Reg_Userform
.
Option Explicit
Dim gReg_Userform As Reg_Userform
Sub conRegBox(control As IRibbonControl)
If Not gReg_Userform Is Nothing Then
On Error Resume Next
If gReg_Userform.ActiveWkbk.FullName <> ActiveWorkbook.FullName Then
Unload gReg_Userform
Set gReg_Userform = Nothing
End If
End If
If gReg_Userform Is Nothing Then
Set gReg_Userform = New Reg_Userform
End If
With gReg_Userform
Set .ActiveWkbk = ActiveWorkbook
.Show
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.