How can I change a Modal Userform from Modal to Modeless at run time ? !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,778
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I am wondering if one can force a Modal UserForm to become Modeless after loading the UserForm.

Load the Userform as Modal => Click on a CommandButton on the Userform => The Userform now becomes Modeless.

Any ideas ?

Regards.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Jaafar,
Try:
Rich (BB code):

Private Sub CommandButton1_Click()
  ' Modeless
  Me.Hide
  Me.Show 0
End Sub

Private Sub CommandButton2_Click()
  ' Modal
  Me.Hide
  Me.Show 1
End Sub
Vladimir
 
Upvote 0
That's nice Vladimir. It never occurred to me to hide and show the userform. I tested the code and it works perfect.

one minor issue is the userform doesn't remember its last screen location but that's easy to solve with a bit of code.

Thank you for your help.

Reagards.
 
Upvote 0
Jaafar,
Here is also an API solution without changing of form’s position and without blinking:
Rich (BB code):

' ZVI:2009-08-09 http://www.mrexcel.com/forum/showthread.php?t=408356
' Switching form to Modal / Modeless at runtime
' Put all code to userform code module
Private Declare Function EnableWindow Lib "user32.dll" (ByVal Hwnd As Long, ByVal fEnable As Long) As Long

Const Modal = 0, Modeless = 1

Private Sub CommandButton1_Click()
  ' Modeless
  EnableWindow Application.Hwnd, Modeless
End Sub

Private Sub CommandButton2_Click()
  ' Modal
  EnableWindow Application.Hwnd, Modal
End Sub
Regards,
Vladimir
 
Upvote 0
Jaafar,
Here is also an API solution without changing of form’s position and without blinking:
Rich (BB code):
' ZVI:2009-08-09 http://www.mrexcel.com/forum/showthread.php?t=408356
' Switching form to Modal / Modeless at runtime
' Put all code to userform code module
Private Declare Function EnableWindow Lib "user32.dll" (ByVal Hwnd As Long, ByVal fEnable As Long) As Long
 
Const Modal = 0, Modeless = 1
 
Private Sub CommandButton1_Click()
 ' Modeless
 EnableWindow Application.Hwnd, Modeless
End Sub
 
Private Sub CommandButton2_Click()
 ' Modal
 EnableWindow Application.Hwnd, Modal
End Sub
Regards,
Vladimir

Thanks vladimir for the interest.

I knew about using the EnableWindow API approach but although it makes possible to select cells it doesn't permit the selection of menus and more importantly excel loses the keyboard focus meaning you can't edit cells.

The challenge is to make this approach work while still allowing full control over the excel application window.

Regards.
 
Upvote 0
Jaafar,

The mentioned by you limitations are absent if the userform initially is loaded as modeless by the manual setting of its property ShowModal to False, or by loading with vbModeless parameter:
Rich (BB code):

Sub Start()
  UserForm1.Show 0  'vbModeless = 0
End Sub

If modal is required at activation then just add to the code of post #4 this:
Rich (BB code):

Private Sub UserForm_Activate()
  EnableWindow Application.hWnd, Modal
End Sub

Regards,
Vladimir
 
Last edited:
Upvote 0
Thanks vladimir.

Maybe i misunderstood you but the EnableWindow API doesn't solve the problem if the userform is initially loaded modal.

The main limitations being the fact that you won't be able to edit the worksheet as you can't set the keyboard focus to the worksheet plus you can't select worksheet menus.

Give it a try and you will see.

I have been trying to solve this challenge for ages but with no luck .

Regards.
 
Upvote 0
Maybe i misunderstood you but the EnableWindow API doesn't solve the problem if the userform is initially loaded modal.
It is true, but you can initially load userform as modeless but in activation event code set it to the modal. In this case you can use ShowWindow API to switch modal / modeless without keyboard blocking and other limitations.

Here is the downloadable working example: ZVI_Modal_Modeless.xls

And its full code:
Code in standard module for loading of UserForm1
Rich (BB code):

Sub Start()
  UserForm1.Show 0  'vbModeless = 0
End Sub

Code in UserForm1 with two command buttons:
Rich (BB code):

' ZVI:2009-08-09 http://www.mrexcel.com/forum/showthread.php?t=408356
' Switching form to Modal / Modeless at runtime
' Code in userform module
Private Declare Function EnableWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal fEnable As Long) As Long

Const Modal = 0, Modeless = 1

Private Sub CommandButton1_Click()
  ' Modeless
  EnableWindow Application.hWnd, Modeless
End Sub

Private Sub CommandButton2_Click()
  ' Modal
  EnableWindow Application.hWnd, Modal
End Sub

Private Sub UserForm_Activate()
  ' Activate as Modal
  EnableWindow Application.hWnd, Modal
End Sub
 
Upvote 0
Vladimir

I don't know if it's really relevant to this but there is no hWnd property of the application in earlier versions of Excel.
 
Upvote 0
For safe exiting of user form this additional code is required in UserForm1:
Rich (BB code):

Private Sub UserForm_Terminate()
  ' Safe termination as Modeless
  EnableWindow Application.hWnd, Modeless
  With Me
    .Hide
    .Show 0
  End With
End Sub

The downloadable example is updated now: ZVI_Modal_Modeless_01.xls

Application.Hwnd is available at least in Excel 2002, 2003, 2007.
It can be replaced by GetParent (FindWindow(vbNullString, Me.Caption))
with API functions:
Rich (BB code):
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetParent Lib "user32.dll" (ByVal hWnd As Long) As Long
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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