Home » excel » vba – Automatically execute some code after the UserForm is drawn

vba – Automatically execute some code after the UserForm is drawn

Posted by: admin March 9, 2020 Leave a comment


I have created a UserForm where the user is required to fill-in three fields. The macro attempts to auto-detect these fields’ values in the UserForm_Initialize() event, then displays the found values in the three fields, but the user can change them. The auto-detection takes a few seconds, though, and delays the appearance of the UserForm. I’d like the UserForm to appear with its fields blank before the auto-detection procedure, then have the auto-detection procedure fill the fields automatically. What would be the best way to do this? Making the UserForm non-modal makes the macro run without waiting for the user’s input, which is problematic. I don’t want to have an “auto-detect” button: this has to be done automatically.

How to&Answers:

Use the Activate() event instead of Initialize() 🙂

Private Sub UserForm_Activate()

End Sub


Thanks! It works, but there seems to be a bug: the dialog is drawn all white until the macro completes. screenshot (the dialog should be gray)

No. It is not a bug 🙂 Try This. Add Doevents as shown below.

Private Sub UserForm_Activate()
    UserForm1.ProgressBar1.Value = 0
    starttime = Timer
    While Timer - starttime < 1
        UserForm1.ProgressBar1.Value = (Timer - starttime) * 100
End Sub




I’d suggest using a timer. Open the form with the input fields disabled and empty, and set the timer to fire within a couple of hundred milliseconds. This should allow the form to be displayed immediately. Do your auto-detection within the timer’s tick event (disable the timer first), then enable the fields and fill in the detected values.


There is a simpler way to do this…

1) On your userform create a new ‘CommandButton’ that will execute the macro you wish to trigger.

2) Set the height and the width of the button to 0

3) Make sure the ‘TabIndex’ parameter for the button is 0… This will create an ‘invisible’ CommandButton that will receive the focus as soon as the form opens.

4) In the calling routine, immediately before the command that ‘shows’ the userform enter the line – ‘Application.SendKeys “~”‘

How it works…

The commandbutton created in (1) is a valid control just like any other wxcept that you can’t see it or click it with the mousebutton. The ‘SendKeys’ command replicates a left mouse key click which is stored in the keyboard buffer until the form displays when it will be read. This has exactly the same effect as a mouse click and will run the required macro.

Incidentally, if you are calling the macro from more than one location and wish to have different actions dependant on the source of the call, you can add more than one ‘invisible’ button and add “{Tab}” before the “~” character to tab the focus through the available controls. e.g. ‘Application.SendKeys “{Tab}~”‘ will activate the button with the ‘TabIndex’ parameter set as 1. ‘Application.SendKeys “{Tab}{Tab}{Tab}{Tab}~” ‘ will activate the button with the ‘TabIndex’ parameter set as 4 etc.