vba 关于用户表单中的选项按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19781459/
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
about option buttons in User form
提问by Mars
I have a question:
我有个问题:
I need to create a user form that contain that usual OK and Cancel Buttons. It also should contain two sets of Options buttons, each set placed inside a frame. The captions on the first set should be basketball, baseball, football, the captions on the second set should be watch on TVand Go to games. I need to write the event handlers and code in a module so that when the program runs, the user sees the form. If the user makes a couple of choices and clicks OK, he should see a message like "Your favorite sport is basketball, and you usually watch on TV." If the user clicks Cancel, the message "Sorry you don't want to play" should appear.
我需要创建一个包含通常的 OK 和 Cancel 按钮的用户表单。它还应该包含两组选项按钮,每组都放置在一个框架内。第一组的字幕应该是篮球、棒球、足球,第二组的字幕应该是看电视和看比赛。我需要在模块中编写事件处理程序和代码,以便在程序运行时,用户可以看到表单。如果用户做出几个选择并单击“确定”,他应该会看到“你最喜欢的运动是篮球,你通常在电视上看”这样的消息。如果用户单击取消,则应显示消息“抱歉,您不想玩”。
I think I almost have it working, but I don't know why I cannot successfully execute the Macro.
我想我几乎让它工作了,但我不知道为什么我不能成功执行宏。
My Code is :
我的代码是:
Option Explicit
Private Sub CommandButton2_Click()
MsgBox ("sorry if you don't want to play")
End Sub
Private Sub commandbuttons_Click()
Dim optbasket As String, optbaseball As String, optfootball As String
Dim optwog As String, optgtg As String
Select Case True
Case optbasket
optbasket = True
Case optbaseball
optbaseball = True
Case optfootball
optfootball = True
End Select
If optwog Then
optwog = True
Else
optgtg = True
End If
btnok = MsgBox("you favorite sport is " & Frame1.Value & "you usually " & Frame2.Value & ",")
End Sub
Private Sub OptionButton1_Click()
End Sub
Private Sub btmcancel_Click()
End Sub
Private Sub btnok_Click()
End Sub
Private Sub Frame1_Click()
End Sub
Private Sub Frame2_Click()
End Sub
Private Sub optbaseball_Click()
End Sub
Private Sub optbasketball_Click()
End Sub
Private Sub optfootball_Click()
End Sub
Thank you very much!!!
非常感谢!!!
回答by sbanders
There are a few things here:
这里有几件事:
- You should name your buttons to be "OkButton" and "CancelButton" or something like that. They'll be easier to track later. Same thing for your radio buttons (baseball, basketball, etc.)
- You don't need your select statement or your if statement
I don't think Frame1 and Frame2 have a .Value property you can call
Here is some sample code. You would add an object to your worksheet that could be clicked on. In this example I just inserted a rectangle object. form the Insert tab. Then in the UserForm code, I renamed the Ok Button to be OkButton and added the function OkButton_click. When it is clicked, I capture the values of the radio buttons. I named them baseball, basketball, and football accordingly as well as watch and go. If one of them is true, then I assign "game" which is a string I declared to be the appropriate title of the game. I did the same thing for whether the person likes to go to the game or watch it. Then I added the CancelButton_Click function to close the userForm.
Private Sub Rectangle1_Click() UserForm1.Show End Sub Private Sub OkButton_Click() Dim game as String, watchOrGo as String If baseball Then game = "baseball" If basketball Then game = "basketball" If football Then game = "football" If watch Then watchOrGo = "watch" If go then watchOrGo = "go" okbtn = Msg("Your favorite sport is " & game & ". You usually " & watchOrGo) End Sub Private Sub CancelButton_Click() cnclbtn = Msg("Sorry you don't want to play") Unload Me End Sub
- 您应该将按钮命名为“OkButton”和“CancelButton”或类似名称。以后会更容易跟踪它们。您的单选按钮(棒球、篮球等)也是如此
- 您不需要 select 语句或 if 语句
我认为 Frame1 和 Frame2 没有您可以调用的 .Value 属性
这是一些示例代码。您可以向工作表中添加一个可以单击的对象。在这个例子中,我只是插入了一个矩形对象。形成插入选项卡。然后在 UserForm 代码中,我将 Ok Button 重命名为 OkButton 并添加了函数 OkButton_click。单击它时,我会捕获单选按钮的值。我相应地将它们命名为棒球、篮球和足球,以及边看边走。如果其中一个是真的,那么我分配“游戏”,这是一个我声明为游戏的适当标题的字符串。我对这个人喜欢去看比赛还是喜欢看比赛做了同样的事情。然后我添加了 CancelButton_Click 函数来关闭用户窗体。
Private Sub Rectangle1_Click() UserForm1.Show End Sub Private Sub OkButton_Click() Dim game as String, watchOrGo as String If baseball Then game = "baseball" If basketball Then game = "basketball" If football Then game = "football" If watch Then watchOrGo = "watch" If go then watchOrGo = "go" okbtn = Msg("Your favorite sport is " & game & ". You usually " & watchOrGo) End Sub Private Sub CancelButton_Click() cnclbtn = Msg("Sorry you don't want to play") Unload Me End Sub
回答by Dan Is Fiddling By Firelight
If you want the code in commandbuttons_Click()
to run when you click OK, you need to put it in the click handler for the OK button: btnok_Click()
; likewise for CommandButton2_CLick()
and btncancel_Click()
.
如果你想在代码中commandbuttons_Click()
当您单击确定运行,你需要把它放在了确定按钮单击处理程序: btnok_Click()
; 同样对于CommandButton2_CLick()
和btncancel_Click()
。