2016-03-17 104 views
1

我正在开发一个用户表单,其中一个部分包含三个指向世界不同部分的复选框。根据这些组合,这些文本值将输入到单元格C9中。Excel - 根据单元格内容设置Userform复选框的值

我想要复选框反映当用户返回到用户窗体时已经在单元格中的内容。我已经能够为用户窗体中的每个其他项目(选项按钮,文本框,组合框)执行此操作,但是我的复选框根本没有响应,它们只是在用户窗体出现时取消选中,而不考虑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 

感谢您的任何帮助。

回答

2

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 tickboxes - 它们在窗体打开时默认为False。

+0

谢谢所有三个答案! (.Me /选择大小写/不显示错误的值),现在正在工作并且更具可读性。 –

相关问题