You can write code in the event procedures of the UserForm or its Controls. For example, code in the Initialize event procedure can initialize variables before the UserForm is displayed.