如果为真,选项 button_click 到 return 文本
Option button_click to return text if true
又是我,
在我的用户表单中,我刚刚添加了 2 个选项按钮单击(1 和 2)。
我需要的是当您选择这些按钮之一来获取单元格中的文本时(单元格中我需要的选项按钮 1 文本是每日检查,选项按钮 2 是每周检查)。
我尝试使用此代码,但它只给我错误或正确的 return 信息。
Private Sub OptionButton1_Click()
If OptionButton1.value = True Then
Range("I2").value = "MONTHLY CHECK"
Else
Range("I2").value = "OPERATIONAL DAY"
End If End sub
然后在选择选项按钮后,我有一个命令按钮,可以将此数据(每日检查或每周检查)输入列 I 的单元格中,每个条目新行都有效,但正如我所说,只有正确或错误的信息
Private Sub CommandButton2_Click()
Dim sh As Worksheet, lastRow As Long
Set sh = Sheets("Test")
lastRow = sh.Range("A" & Rows.Count).End(xlUp).row + 1
sh.Range("I" & lastRow).value = OptionButton1.value
sh.Range("I" & lastRow).value = OptionButton2.value
Unload Me
End Sub
提前感谢您一如既往的帮助。
根据我的评论,问题是您将 sh.Range
值分配给 OptionButton.Value
。
根据 OptionButton.Value property; 确定或指定指定的选项按钮是否被选中...
.Value
属性 returns True/False 如果OptionButton是Selected/Unselected分别.
注意:您还可以通过在代码中设置 .Value
属性 将 OptionButton 的状态更改为 select/unselect。
OptionButton.Caption 属性(see here for label.caption 概述了选项按钮标题的相同原则)是用户可见的文本,通常描述选项按钮的用途或用途。
所以您的代码 returning True/False 因为您使用的是 .Value
属性 - 而不是使用 .Caption
属性 到 return 分配给该选项按钮的描述性文本。
示例:
这是一个带有一个 OptionButton 的新用户窗体,两者都具有默认名称。
OptionButton1.Value
是 True
AND
OptionButton1.Caption
是 This is OptionButton1.
如果我们要取消选择选项按钮(比如通过执行一些 'Reset' 代码),OptionButton1.Value
将是 False
但 OptionButton1.Caption
将保持 This is OptionButton1.
又是我,
在我的用户表单中,我刚刚添加了 2 个选项按钮单击(1 和 2)。 我需要的是当您选择这些按钮之一来获取单元格中的文本时(单元格中我需要的选项按钮 1 文本是每日检查,选项按钮 2 是每周检查)。
我尝试使用此代码,但它只给我错误或正确的 return 信息。
Private Sub OptionButton1_Click()
If OptionButton1.value = True Then
Range("I2").value = "MONTHLY CHECK"
Else
Range("I2").value = "OPERATIONAL DAY"
End If End sub
然后在选择选项按钮后,我有一个命令按钮,可以将此数据(每日检查或每周检查)输入列 I 的单元格中,每个条目新行都有效,但正如我所说,只有正确或错误的信息
Private Sub CommandButton2_Click()
Dim sh As Worksheet, lastRow As Long
Set sh = Sheets("Test")
lastRow = sh.Range("A" & Rows.Count).End(xlUp).row + 1
sh.Range("I" & lastRow).value = OptionButton1.value
sh.Range("I" & lastRow).value = OptionButton2.value
Unload Me
End Sub
提前感谢您一如既往的帮助。
根据我的评论,问题是您将 sh.Range
值分配给 OptionButton.Value
。
根据 OptionButton.Value property; 确定或指定指定的选项按钮是否被选中...
.Value
属性 returns True/False 如果OptionButton是Selected/Unselected分别.
注意:您还可以通过在代码中设置 .Value
属性 将 OptionButton 的状态更改为 select/unselect。
OptionButton.Caption 属性(see here for label.caption 概述了选项按钮标题的相同原则)是用户可见的文本,通常描述选项按钮的用途或用途。
所以您的代码 returning True/False 因为您使用的是 .Value
属性 - 而不是使用 .Caption
属性 到 return 分配给该选项按钮的描述性文本。
示例:
这是一个带有一个 OptionButton 的新用户窗体,两者都具有默认名称。
OptionButton1.Value
是 True
AND
OptionButton1.Caption
是 This is OptionButton1.
如果我们要取消选择选项按钮(比如通过执行一些 'Reset' 代码),OptionButton1.Value
将是 False
但 OptionButton1.Caption
将保持 This is OptionButton1.