[英]How to autofill the excel form based on the cell in the active worksheet using VBA?
[英]How to autofill the option button in excel user form based on a cell value?
将此添加到您的用户表单模块中:
Private Sub UserForm_Initialize()
If Sheets("Sheet1").Range("B4") = "Profit" Then Me.ProfitOption.Value = True
End Sub
几个要改变的事情:
- 将Sheet1
更改为您的。
- 将ProfitOption
为按钮的名称。
我推荐这样的东西
Private Sub UserForm_Initialize()
Select Case Sheet1.Range("B4").Value 'evaluate the value of the cell
Case "Profit"
Me.OptionButton1.Value = True
Case "Loss"
Me.OptionButton2.Value = True
Case Else 'if it is none of the above then go into undefined state
Me.OptionButton1.Value = Null
Me.OptionButton2.Value = Null
End Select
End Sub
请注意,如果您更改用户表单中的选项,这不会更改单元格值。 因此,您需要使用Private Sub OptionButton1_Change()
事件或“保存”按钮将更改后的 state 写回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.