Excel - 根据单元格内容设置用户表单复选框的值

Excel - Set values of Userform Checkboxes based on cell contents

我正在开发一个用户表单,其中一个部分包含三个复选框,分别代表世界的不同地区。根据这些组合,在单元格 C9 中输入文本值。

我想让复选框反映用户返回用户窗体时单元格中已有的内容。我已经能够为用户窗体中的所有其他项目(选项按钮、文本框、组合框)执行此操作,但我的复选框根本没有响应,当用户窗体出现时,它们只是未选中,无论 C9 的值如何。

以下代码在userform_intialize模块中。有什么想法吗?

If wsM.Range("C9").Value = "EU-5" Then
        NABox.Value = False And EUBox.Value = True And RoWBox.Value = False
    ElseIf wsM.Range("C9").Value = "EU-5 & RoW" Then
        NABox.Value = False And EUBox.Value = True And RoWBox.Value = True
    ElseIf Sheets("Menu").Range("C9").Value = "NA & EU-5" Then
        NABox.Value = True And EUBox.Value = True And RoWBox.Value = False
    ElseIf wsM.Range("C9").Value = "North America" Then
        NABox.Value = True And EUBox.Value = False And RoWBox.Value = False
    ElseIf wsM.Range("C9").Value = "NA & RoW" Then
        NABox.Value = True And EUBox.Value = False And RoWBox.Value = True
    ElseIf wsM.Range("C9").Value = "Rest of World" Then
        NABox.Value = False And EUBox.Value = False And RoWBox.Value = True
    Else: NABox.Value = False And EUBox.Value = False And RoWBox.Value = False
End If

感谢您的帮助。

Me. 关键字放在复选框名称前面。
也可能更好地使用 SELECT CASE 语句而不是 ElseIf.

NABox.Value = False And EUBox.Value = True And RoWBox.Value = False 需要三个单独的命令。要么在单独的行上,要么用 : 拆分(下面代码中的两个示例)。

Private Sub UserForm_Initialize()

    With Me
        Select Case wsm.Range("C9").Value
            Case "EU-5"
                NABox.Value = False
                EUBox.Value = True
                RoWBox.Value = False
            Case "EU-5 & RoW"
                NABox.Value = False : EUBox.Value = True
                RoWBox.Value = False
            Case "NA & EU-5"

            Case Else

        End Select
    End With

End Sub

编辑 - 我认为您不需要明确声明 False 复选框 - 当表单打开时它们默认为 False。