Excel VBA IF然后IF语句

时间:2017-10-16 13:12:55

标签: vba excel-vba if-statement msgbox excel

我正在使用“If Then If msgbox vbYesno”声明,我不知道如何正确地解决它(我知道Goto不干净)。

有人可以告诉我我的错误是什么吗?我没有发现任何人使用类似的东西。

Sub IF_THEN_IF()

If Sheet1.Range("A1").Value > 500 Then
    If MsgBox("A1 > 500, Is this correct", vbYesNo, "Amount of Lines") = vbYes Then
        Range("H11").FormulaR1C1 = "My Formula"
    Else
        GoTo Jump
    End If
Else

Jump:    
    Range("H11").FormulaR1C1 = "I have Jumped"

End If

End Sub

4 个答案:

答案 0 :(得分:2)

如果你不打算“跳”,你可以离开你的程序,跳到其他地方:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<link href="Styles/iziModal.min.css" rel="stylesheet" type="text/css" />
<script src="Scripts/izimodal.js"></script>
</head>
<body>
<div id="Toolbar" width="auto"></div>

<div class="btn btn-danger MODAL1" style="padding-top: 100px;">Modal 1</div>
<div class="btn btn-danger MODAL2" style="padding-top: 100px;">Modal 2</div>

<div id="modal" class="iziModal">        
    <p>This is the Modal content</p>
</div>
</body>
<script>

$(document).ready(function () {    

$('.MODAL1').on("click", function (e) {
   ShowModal("This is Modal 1");
});

$('.MODAL2').on("click", function (e) {
   ShowModal("This is Modal 2");
});
});

function ShowModal(Title)
{
$("#modal").iziModal({
    title: Title,
    subtitle: "Simple, complete and lightweight modal plugin with jquery.",
    iconClass: 'icon-stack',
    overlayColor: 'rgba(255, 255, 255, 0.4)',
    width: 700,
    padding: 20
});

$('#modal').iziModal('open');
}
</script>
</html>

另一个选择是使用布尔变量:

Sub IF_THEN_IF()
    If Sheet1.Range("A1").Value > 500 Then
        If MsgBox("A1 > 500, Is this correct", vbYesNo, "Amount of Lines") = vbYes Then
            Range("H11").FormulaR1C1 = "My Formula"
            Exit Sub
        End If
   End If
   'Jump
    Range("H11").FormulaR1C1 = "I have Jumped"
End Sub

答案 1 :(得分:1)

这个回答是对你对Vityaya回答的评论的回应。

将代码从Jump转换为子例程,并根据需要调用它。

Sub IF_THEN_IF()

With Sheet1 

    If .Range("A1").Value > 500 Then

        Dim res as Variant
        res = MsgBox("A1 > 500, Is this correct", vbYesNo, "Amount of Lines")

        If res = vbYes Then
            .Range("H11").FormulaR1C1 = "My Formula"
        Else
            Jump
        End If

    Else   

        Jump

    End If

End Sub

Sub Jump()
   'code here
End Sub

答案 2 :(得分:0)

可能的选择是这样的:

In [2781]: df.pivot_table(index=['ID_Patient', 'Name_Patient','month','year'], 
                          columns='visits', values='var').reset_index()
Out[2781]:
visits  ID_Patient Name_Patient month  year    A    U    Z
0             2755         Jack   Apr  2012  3.0  1.0  1.0
1             9753          Sue   Feb  2012  NaN  NaN  1.0
2             9753          Sue   Jan  2012  5.0  8.0  NaN
3            11132          Jim   Feb  2012  4.0  NaN  6.0
4            11132          Jim   Jan  2012  2.0  5.0  NaN

你有两个嵌入式ifs。就这样。在Excel VBA IF then IF statement @Scott Holtzmann的回答中,您可以看到如何通过遵循DRY method来避免写“我已经跳过”两次。

答案 3 :(得分:0)

您可以使用AND运算符

Sub IF_THEN_IF()    
If Sheet1.Range("A1").Value > 500 And _
   MsgBox("A1 > 500, Is this correct", vbYesNo, "Amount of Lines") = vbYes Then

    Range("H11").FormulaR1C1 = "My Formula"

Else

    Range("H11").FormulaR1C1 = "I have Jumped"

End Sub