vba 用户表单禁用所有字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16533259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Userform disable all fields
提问by Haissam
I have a userform that can be filled in but only if a previous userform was filled in as there are calculations in the userform based on the previous input and if those are empty, the calculations crash.
我有一个可以填写的用户表单,但前提是填写了先前的用户表单,因为用户表单中存在基于先前输入的计算,如果这些为空,则计算会崩溃。
Now I did write a few if statements that check for these empty values and then had a brain flash.. how about if one of the fields is missing, the userform is just disabled. So thought, so done and it works :)
现在我确实写了一些 if 语句来检查这些空值,然后脑中闪过……如果其中一个字段丢失了,用户表单就被禁用了。这么想,这么干,它的工作原理:)
if DP1 = "" then
reportback.enable = false
else
end if
That is the form and it shows up beautifully and nothing can be changed, but Ohhh.. you can′t even close the form, nothing works .. lol.
这就是表格,它显示得很漂亮,没有什么可以改变的,但是哦..你甚至不能关闭表格,什么都不起作用..大声笑。
So my question. Is there a way to disable all fields from any input but still have the cancel button active ?
所以我的问题。有没有办法禁用任何输入的所有字段,但仍然激活取消按钮?
Private Sub Cancel_Click()
Unload reportback
End Sub
回答by Santosh
Use below code to disable all controls on your form to avoid the issue. UserForm1 refers to name of Userform kindly replace accordingly.
使用以下代码禁用表单上的所有控件以避免出现此问题。UserForm1 是指 Userform 的名称,请相应替换。
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
ctrl.Enabled = False
Next
Set ctrl = Nothing