VBA – IF语句与AND不工作

任何帮助将是伟大的。 我似乎不能用AND来得到ELSEIF。

我正在使用一个用户的forms。 当他们按下button时,用户窗体显示checkbox。 当两个checkbox都被选中时,我遇到了麻烦。 单独他们工作正常,

我不知道我做错了什么。 任何帮助将不胜感激

If Intact.Value = True Then storDate = Sheets("escalation").Cells(Selection.Row, 2) storProject = Sheets("escalation").Cells(Selection.Row, 3) StorBill = Sheets("escalation").Cells(Selection.Row, 4) storIntact = "Intact" With objWord.ActiveDocument .formfields("text2").Result = storDate .formfields("Text3").Result = storProject .formfields("Text4").Result = StorBill .formfields("Text9").Result = storIntact End With ElseIf Compugen.Value = True Then storDate = Sheets("escalation").Cells(Selection.Row, 2) storProject = Sheets("escalation").Cells(Selection.Row, 3) StorBill = Sheets("escalation").Cells(Selection.Row, 4) storCompugen = "Compugen" With objWord.ActiveDocument .formfields("text2").Result = storDate .formfields("Text3").Result = storProject .formfields("Text4").Result = StorBill .formfields("Text9").Result = storCompugen End With ElseIf Intact.Value And Compugen.Value = True Then storDate = Sheets("escalation").Cells(Selection.Row, 2) storProject = Sheets("escalation").Cells(Selection.Row, 3) StorBill = Sheets("escalation").Cells(Selection.Row, 4) storIntact = "Intact" storDate1 = Sheets("escalation").Cells(Selection.Row, 2) storProject1 = Sheets("escalation").Cells(Selection.Row, 3) StorBill1 = Sheets("escalation").Cells(Selection.Row, 4) storCompugen = "Compugen" With objWord.ActiveDocument .formfields("text2").Result = storDate .formfields("Text3").Result = storProject .formfields("Text4").Result = StorBill .formfields("Text9").Result = storIntact .formfields("text5").Result = storDate1 .formfields("Text6").Result = storProject1 .formfields("Text7").Result = StorBill1 .formfields("Text8").Result = storCompugen End With End If 

提前致谢

尝试改变顺序。 否则只要满足一个条件,If子句退出。

 If Intact.Value And Compugen.Value Then 'code ElseIf Intact.Value Then 'code ElseIf Compugen.Value Then 'code End If 

如果Intact.Value = True ,那么第一个,而不是第三个块将运行。

同样,如果Intact.Value = True不正确, Compugen.Value = True则为true,则第二个块将运行。

所以你可以看到第三个块不可访问。

解决方法是将Intact.Value = True And Compugen.Value = True情况放在组中。

最后, Foo.Value = True是更简单的Foo.Value一个重言式。 你可以删除所有的显式= True比较。