[VBA] How to Automatically Close a UserForm After Execution | Using Unload Me

目次

Background

When creating tools using UserForms in Excel, I was bothered by the fact that the form remained on the screen even after the program was executed.

It looked cluttered, and I wanted to close the form to clearly indicate that the process was complete. In this article, I will explain how to automatically dismiss a UserForm after the program finishes running.

Solution: Use Unload Me

In VBA, you can close a UserForm by using the Unload Me command.

For example, if you have processing logic written in a “Run” button on the form, you simply need to add Unload Me at the very end of that code.

Sample Code

Private Sub CommandButton1_Click()
    ' Write some processing here
    MsgBox "Processing is complete."

    ' Close the form
    Unload Me
End Sub

By doing this, the form will automatically close after the process is finished, so no further action is required from the user.

Supplement: Meaning of Unload Me

  • Unload: This is a command to completely destroy the form (object) and remove it from memory.
  • Me: This refers to the form itself that is currently running.

Therefore, Unload Me translates to “Completely close this form.”

Summary

If you want to automatically close a UserForm after execution, simply write Unload Me at the end of your procedure.

  • It makes the screen look cleaner.
  • It serves as a clear signal that the process has finished.
  • It is convenient to use actively when the form does not need to remain open.

Although it is a small detail, it significantly affects usability and improves the user experience. Please try incorporating it into your projects.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次