简体   繁体   中英

How to show userform, then hide, then show again without running the userform activate code again

I have a progress using a userform. I show the userform when a worksheet button is clicked. The button calls startProgressIndicator()

Sub startProgressIndicator()
    UserForm1.Show
End Sub

I have this as my userform code:

Private Sub UserForm_Activate()
    Call myCode
End Sub

I then want to hide the progress bar and ask for user input. This occurs in myCode . I inlcude UserForm1.Hide in beginning of myCode .

After getting the user input, I want to show the progress indicator again. I try UserForm1.Show , however, this just calls myCode all over again. I just want the same userform to be visible again.

I tried using UserForm1.Visible = False , but then I get this error

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic

Short answer is to rewrite myCode to not include .Hide . Break myCode into logical chunks.

However, you should separate the logic from the display (see Rubberduck UserForm1.Show (*)). By doing so - you would only call .Hide from the form when you want to (eg on the 'Close' button click).

@ChrisNeilsen suggested using _Initialize and this will solve the immediate problem but will not set you up for better programming practices in the future. lso, if you decide to modify myCode you may get bugs that are harder to identify.

@ChrisNeilsen: Use 'UserForm_Initialize' rather than 'UserForm_Activate'

(*) No disclaimer required, I am in no way affiliated with Rubberduck, but it does make good reading!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM