如果条件不满足但代码仍然执行

时间:2016-03-20 15:47:46

标签: excel vba excel-vba

我对以下代码略有挑战。不满足If子句中的条件,但代码仍会触发。有人知道我能做些什么吗? (我已经尝试过使用If Not Statement,但这也不会奏效。)

对于我在空闲时间做的学习和/或工作,这应该是一个小工作时间跟踪器。

Option Explicit
Dim Timestamp As Date
Dim myProject As String
Dim myWTI As Integer
Dim myPayment As String
Dim myPaymentDetail As String
Dim i As Integer
Public Sub StartTime()

Application.ScreenUpdating = False

myProject = [cell_Project]
myWTI = [cell_WorktimeInterval]
myPayment = [cell_Payment]
myPaymentDetail = [cell_Paymentdetail]

If myWTI Or myProject = Empty Then
    MsgBox "No Project and/or WTI chosen.", vbCritical, "Error: No Project/WTI"
    Exit Sub
Else (rest of the code - this runs perfectly fine without this If-clause)

1 个答案:

答案 0 :(得分:3)

在VBA中,你不能写像

这样的东西
If myWTI Or myProject = Empty Then

这没有任何意义。 你需要这样的东西:

If myWTI = 0 Or myProject = "" Then

另请注意,myInt声明为Integer,myProject声明为String。因此,它们不能是" null"或"清空",它们总是需要分配一个值,如0或""。

相关问题