如果/ Then语句没有正确执行

时间:2017-10-12 09:13:17

标签: excel vba excel-vba if-statement

我用If / Then创建了各种宏但这次我遇到了If / Then进程的问题。在And之后的If / Then的第二部分没有正确执行。它只会为And之前的偏移量转0。单元格的格式没有问题,因为当我改变它们的位置时,它可以正常地用于之前的那个。我提供以下代码。

For Each cell2 In Range("E7:E" & lastrow12)
    If cell2.Value = 0 Then
    cell2.Offset(0, -2).Value = 0 And cell2.Offset(0, -1).Value = 0
    End If
    Next cell2

2 个答案:

答案 0 :(得分:1)

你走了:

For Each cell2 In Range("E7:E" & lastrow12)
    If cell2.value = 0 Then
        cell2.Offset(0, -2).value = 0
        cell2.Offset(0, -1).value = 0
    End If
Next cell2

And is a boolean operator, it is used in conditions

答案 1 :(得分:1)

And函数是一个内置函数,被归类为逻辑函数,例如True和True。

您不能用于合并两个语句。那说:

For Each cell2 In Range("E7:E" & lastrow12)
    With cell2
        If .Value = 0 Then
            .Offset(0, -1).Value = 0
            .Offset(0, -2).Value = 0 
        End If
    End With
Next cell2
相关问题