Question "Run-time error 402 -- Must close topmost modal form first" - Excel VB ?

cve60069

Distinguished
Jul 15, 2010
44
4
18,535
Hello

I am using the latest Excel on a Windows 11 PC. I hace created a UserForm and when I try to hide the userform I get the following error. - "Run-time error '402' Must close topmost modal form first"

Looking on the Internet I get the advise that something broke in my PC when I had a sudden shutdown and I should install a PC Repair tool. My PC is very new and I do not want to install this software.

Any clues on what is happening please?

Dan
 

Colif

Win 11 Master
Moderator
right click start
choose terminal (admin)
if it doesn't open as Powershell, click the drop down arrow in the title and choose from list
copy/paste this command into window:

Repair-WindowsImage -Online -RestoreHealth

and press enter

Then type SFC /scannow

and press enter


Restart PC if SFC fixes any files as some fixes require a restart to be implemented

First command repairs the files SFC uses to clean files, and SFC fixes system files

SFC = System File Checker. First command runs DISM - https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/what-is-dism?view=windows-11
 

Colif

Win 11 Master
Moderator
The modal form you are trying to close or hide isn't on top of the z-order. This error has the following cause and solution:
  • Another modal form is higher in the z-order than the modal form you tried to close or hide. First use either the Unload statement or the Hide method on any modal form higher in the z-order. A modal form is a form displayed by the Show method, with the style argument set to 1 - vbModal.

https://learn.microsoft.com/en-us/o...se-or-hide-topmost-modal-form-first-error-402
 
D

Deleted member 14196

Guest
Most likely, the problem is your code, and since we can’t look at your VB code, we have no idea what you’re doing. Do you open any other forms first? And if so, if they are Modal Forms , they will need to be hidden or closed first

this has nothing to do with your system being broken. If it was broken office wouldn’t run.
 

cve60069

Distinguished
Jul 15, 2010
44
4
18,535
Thank you for the replies.

Colif - thank you for your help. I ran the Restore Health and SFC and the software detected problems which the software fixed. Did not solve my problem.

Mandark

This is the code that failed:

Private Sub CommandButton1_Click()
NAGdata.Hide
End Sub

And this is the code that opens the form

Sub DoNAGproject()

Dim form As New NAGdata
form.Show
Unload form

End Sub

And there is the error. I changed the code to

Sub DoNAGproject()

NAGdata.Show
Unload NAGdata

End Sub

And it worked. The error was with the New statement.

The problem is solved.