我正在使用vba excel用户窗体,但是我陷入了这样的困境:在某些情况下,名为“ Textbox6”的文本框不会更新-就像是否选择了选项按钮。
换句话说。如果Optionbuttton10为“仅”,则Textbox6在单元格D3中显示该值。
如果“ OptionButton10”为true并且“ Optionbutton2”为True,那么我希望Textbox6显示单元格E3
这是我到目前为止所拥有的代码,它看起来“应该”可以正常工作,但是我缺少一些东西。
Private Sub OptionButton10_Click()
Dim D3 As Variant
Dim E3 As Variant
D3 = Sheets("Systems").Range("D3").Value
E3 = Sheets("Systems").Range("E3").Value
If OptionButton10 = True And ComboBox2 = "Standard" Or ComboBox2 = "Scale-In" Then
TextBox6.Value = D3 'this works'
ElseIf OptionButton10 = True And OptionButton2 = True Then
TextBox6.Value = E3 'this doesn't work'
End If
TextBox6.Text = Format(TextBox6.Value, "Percent")
End Sub
答案 0 :(得分:0)
很难确定,但是根据您的评论,您可能需要尝试以下一种方法:
Private Sub OptionButton10_Click()
Dim D3 As Variant
Dim E3 As Variant
D3 = Sheets("Systems").Range("D3").Value
E3 = Sheets("Systems").Range("E3").Value
If (OptionButton10 And Not (OptionButton2)) And (ComboBox2 = "Standard" Or ComboBox2 = "Scale-In") Then
TextBox6.Value = D3 'this works'
ElseIf OptionButton10 = True And OptionButton2 = True Then
TextBox6.Value = E3 'this doesn't work'
End If
TextBox6.text = Format(TextBox6.Value, "Percent")
End Sub
或者,或者:
Private Sub OptionButton10_Click()
Dim D3 As Variant
Dim E3 As Variant
D3 = Sheets("Systems").Range("D3").Value
E3 = Sheets("Systems").Range("E3").Value
If OptionButton10 And OptionButton2 Then
TextBox6.Value = E3 'this doesn't work'
ElseIf OptionButton10 = True And (ComboBox2 = "Standard" Or ComboBox2 = "Scale-In") Then
TextBox6.Value = D3 'this works'
End If
TextBox6.text = Format(TextBox6.Value, "Percent")
End Sub
此外,我强烈建议您为按钮和框(包括文本框)命名。将来,当您查看代码并阅读以下内容时,将不胜感激:
If (ProcessFoo and Not(ProcessBar)) and (Baz = "Standard" or Baz = "Scale-In")
*带有用于选项和组合框的适当名称,如果将它们命名为Foo
和Bar
和Baz
,那么将来您可能会比离开时更加困惑它们具有默认名称...
与您需要回到表单中查看OptionButton10
和OptionButton2
的文本标签相比,这将使您更快地理解。
答案 1 :(得分:0)
对我有用的解决方案如下。
我使用了一个嵌套的If语句,并添加了“ optionbutton2 = false”
Private Sub OptionButton10_Click()
Dim D3 As Variant
Dim E3 As Variant
D3 = Sheets("Systems").Range("D3").Value
E3 = Sheets("Systems").Range("E3").Value
If ComboBox2 = "Standard" Or ComboBox2 = "Scale-In" Then
If OptionButton10 = True And OptionButton2 = False Then
TextBox6.Value = D3
ElseIf OptionButton10 = True And OptionButton2 = True Then
TextBox6.Value = E3
End If
End If
TextBox6.Text = Format(TextBox6.Value, "Percent")
End Sub