简体   繁体   English

如何显示用户表单,然后隐藏,然后再次显示而不再次运行用户表单激活代码

[英]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()该按钮调用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 .这发生在myCode中。 I inlcude UserForm1.Hide in beginning of myCode .我在myCode的开头包含UserForm1.Hide

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.我尝试了UserForm1.Show ,但是,这只是再次调用myCode I just want the same userform to be visible again.我只想再次看到相同的用户表单。

I tried using UserForm1.Visible = False , but then I get this error我尝试使用UserForm1.Visible = False ,但随后出现此错误

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic Function 或标记为受限的接口,或 function 使用 Visual Basic 不支持的自动化类型

Short answer is to rewrite myCode to not include .Hide .简短的回答是重写myCode以不包括.Hide Break myCode into logical chunks. myCode分解为逻辑块。

However, you should separate the logic from the display (see Rubberduck UserForm1.Show (*)).但是,您应该将逻辑与显示分开(请参阅Rubberduck UserForm1.Show (*))。 By doing so - you would only call .Hide from the form when you want to (eg on the 'Close' button click).通过这样做 - 您只会在需要时从表单中调用.Hide (例如,单击“关闭”按钮)。

@ChrisNeilsen suggested using _Initialize and this will solve the immediate problem but will not set you up for better programming practices in the future. @ChrisNeilsen 建议使用_Initialize ,这将解决眼前的问题,但不会让您在未来获得更好的编程实践。 lso, if you decide to modify myCode you may get bugs that are harder to identify.此外,如果您决定修改myCode ,您可能会遇到难以识别的错误。

@ChrisNeilsen: Use 'UserForm_Initialize' rather than 'UserForm_Activate' @ChrisNeilsen:使用“UserForm_Initialize”而不是“UserForm_Activate”

(*) No disclaimer required, I am in no way affiliated with Rubberduck, but it does make good reading! (*) 不需要免责声明,我与 Rubberduck 没有任何关系,但它确实值得阅读!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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