vba 使用预设值初始化用户表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25952419/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:36:44  来源:igfitidea点击:

Initializing a userform with preset values?

vbaexcel-vbaradio-buttoninitializationuserform

提问by Max Bessey

Working in VBA for excel 2010. This is my first time working with VBA and userforms. Right now I have a barebones userform "UserForm1" trying to sort this issue out. It consists of two radio buttons "OptionButton1" and "OptionButton2" belonging to GroupName "WK" and two textboxes "TextBox1" and "TextBox2".

在 Excel 2010 中使用 VBA。这是我第一次使用 VBA 和用户表单。现在我有一个准系统用户表单“UserForm1”试图解决这个问题。它由属于 GroupName "WK" 的两个单选按钮 "OptionButton1" 和 "OptionButton2" 以及两个文本框 "TextBox1" 和 "TextBox2" 组成。

When I run the userform, I want "OptionButton1" to be selected and the subsequent if/then statements to be run. However, right now I cannot get it to do this. My code:

当我运行用户表单时,我希望选择“OptionButton1”并运行后续的 if/then 语句。但是,现在我无法做到这一点。我的代码:

Public Sub UserForm1_Initialize()
UserForm1.Show
Me.OptionButton1.Value = False
Me.OptionButton1.Value = True
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End Sub

Public Sub UserForm1_Activate()

End Sub

Public Sub OptionButton1_Click()

If Me.OptionButton1.Value = True Then
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End If

End Sub

Private Sub TextBox1_Change()

End Sub

When I run the form, nothing happens and "OptionButton1" is false. If I click it, the message box displays and the textbox displays the text. It just won't do it on startup. I have tried going into the optionbutton properties and setting 'value' to true, which makes it true on startup, but the messagebox still doesn't display and the textbox is blankuntil I click it.

当我运行表单时,没有任何反应并且“OptionButton1”是假的。如果我单击它,消息框会显示,文本框会显示文本。它只是不会在启动时这样做。我尝试进入选项按钮属性并将“值”设置为 true,这使其在启动时为 true,但消息框仍然不显示,并且文本框为空白,直到我单击它。

...please help. Thank you.

...请帮忙。谢谢你。

回答by Max Bessey

I figured it out.

我想到了。

I suddenly found the dropdowns. Apparently I should have put Userform_Initialize() instead of UserForm1_Initialize(), and instead of OptionButton1_Click() I put the code into OptionButton1_Change() which ran the subsequent initialization sequence.

我突然发现了下拉菜单。显然我应该把 Userform_Initialize() 而不是 UserForm1_Initialize(),而不是 OptionButton1_Click() 我把代码放到 OptionButton1_Change() 中,它运行了后续的初始化序列。

You guys/gals are awesomesauce. I have learned everything from reading your threads. Thank you!

你们这些家伙/女孩真是太棒了。我从阅读您的主题中学到了一切。谢谢!