【问题标题】:How to autofill the option button in excel user form based on a cell value?如何根据单元格值自动填充excel用户表单中的选项按钮?
【发布时间】:2021-01-12 08:58:32
【问题描述】:

嗨,

我想让用户窗体能够检测单元格B4 中的文本并选择正确的选项按钮,而无需在用户窗体中进行任何额外的点击。我可以知道我应该如何实现吗?谢谢。

【问题讨论】:

    标签: excel vba forms autofill


    【解决方案1】:

    将此添加到您的用户表单模块中:

    Private Sub UserForm_Initialize()
    
    If Sheets("Sheet1").Range("B4") = "Profit" Then Me.ProfitOption.Value = True
    
    End Sub
    

    几件事要改变:

    -将Sheet1 更改为您的。

    -将ProfitOption 更改为按钮的名称。

    【讨论】:

    • 实际上Me.ProfitOption.ValueBoolean 而不是String 因此应该是Me.ProfitOption.Value = True
    • 我正要发表评论,但不管怎样都行。
    • 好吧“意外工作” != “工作因为它是正确的” :)
    【解决方案2】:

    我推荐这样的东西

    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() 事件或“保存”按钮将更改的状态写回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多