VBA的If则语句做东西或结束子

时间:2018-11-29 18:12:45

标签: excel vba excel-vba

我在编写简单代码时遇到麻烦。我想添加If Then语句,如果满足条件(单元格值不是1或7),请执行一段代码,否则结束子程序。代码块包括登录网站和2 For Next循环。基本上,宏将在工作日运行,而不是在周六或周日运行。感谢您的帮助。

这是一个测试代码:

Sub test()

Dim i As Integer

'If cell E1 has a value of neither 1 or 7, Do stuff, otherwise End the Sub

If Cells(5, 1) <> 1 Or Cells(5, 1) <> 7 Then

    'Do stuff includes login and perform 2 For Next loops in my real code 
    For i = 1 To 3
        Cells(i, 1).Value = Cells(i, 1).Value * 2
    Next

End If

End Sub

2 个答案:

答案 0 :(得分:0)

尝试

Sub test()

    Dim i As Integer

    'If cell E1 has a value of neither 1 or 7, Do stuff, otherwise End the Sub

    If Cells(5, 1) = 1 Or Cells(5, 1) = 7 Then
    Else
        'Do stuff includes login and perform 2 For Next loops in my real code
        For i = 1 To 3
            Cells(i, 1).Value = Cells(i, 1).Value * 2
        Next

    End If

End Sub

答案 1 :(得分:0)

我终于能够实现我想要的。我创建了一个新的子查询,它检查星期几,如果是星期六或星期日,然后退出子查询,否则调用我原来的子查询,效果很好。但是,正如Comintern建议的那样,我将研究VBA的检查日期功能,而不是使用电子表格。谢谢大家。