This isn’t the default way to show a UserForm.Īnyway, pretend you have this modeless UserForm floating around. You can interact with things other than your UserForm.
Remember, the Workbooks collection contains all the Workbook objects that are currently open. Sub sbWriteCellWhenClosing() Workbooks('BOOK1.XLS').Close SaveChanges:True Range('G2') Format(Now - 2, dd - mm - yy) End Sub. Let’s say you have a modeless UserForm (ShowModal property set to False), titled UserForm1. The Close and Open Method in Excel VBA can be used to close and open workbooks. I used it to demonstrate my VBA Excel splash screen, but if you recall, I didn’t use the “Me” keyword. This isn’t the first time you’ve seen me use the Unload statement. “Unload Me” closes your form and removes everything associated with it from memory.
Feel free to run whatever commands you want before you put the Unload Me statement. Your macro can be a lot more complicated than this. The only new item is the Filename parameter (4). This sample macro is very similar to the previous example 3. Specifies that the filename that Excel uses is Excel VBA Close Workbook. When you click the cbCancel button, the form is closed, because I double clicked the cbCancel button during development and pasted the example macro for the click event. Filename:Excel VBA Close Workbook: The Filename argument of the Close method. On this form, I’ve named the Cancel button cbCancel. You may recognize this UserForm from my Mouse To Macro mouse recording Excel Add-in. Pretend you have a userform that looks like this: Let’s walk through an example showing where it’s appropriate to use “Unload Me” to close your form. In other words, if you type Unload Me in a regular module, it will fail with an error saying “Invalid use of Me keyword.” Remember, the module doesn’t know what UserForm you want to unload! That’s because only the UserForm knows that “Me” is the UserForm. The macro above only works when you call it from an event procedure on that UserForm - like a command button.
Tutorial - VBA Close UserForm Close a UserForm from UserForm Event