首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA - IF语句与和不起作用

VBA - IF语句与和不起作用
EN

Stack Overflow用户
提问于 2017-03-09 00:44:51
回答 2查看 1.4K关注 0票数 0

任何帮助都是最好的。我似乎不能让ELSEIF与AND一起工作。

我正在使用一个用户表单。当他们按下该按钮时,将出现带有复选框的用户窗体。当两个复选框都选中时,我遇到问题。它们单独工作得很好,

我不确定我做错了什么。任何帮助都将不胜感激

代码语言:javascript
复制
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

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2017-03-09 00:51:24

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

代码语言:javascript
复制
If Intact.Value And Compugen.Value Then
    'code
ElseIf Intact.Value Then
    'code
ElseIf Compugen.Value Then
    'code
End If
票数 2
EN

Stack Overflow用户

发布于 2017-03-09 00:49:16

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

类似地,如果Intact.Value = True不为true,而Compugen.Value = True为true,那么第二个块将运行。

因此,您可以看到第三个块是不可访问的。

解决方案是将Intact.Value = True And Compugen.Value = True案例放在组中的第一位。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42677067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档