Vba If/Then语句执行不正确

Vba If/Then语句执行不正确,vba,excel,if-statement,Vba,Excel,If Statement,我用If/Then创建了各种宏,但这次我遇到了If/Then进程的问题。在和之后,它不能正确执行If/Then的第二部分。它仅将之前和之后的偏移值转换为0。单元格的格式没有问题,因为,当我改变它们的位置时,它可以很好地用于之前和之后的单元格。我提供下面的代码 For Each cell2 In Range("E7:E" & lastrow12) If cell2.Value = 0 Then cell2.Offset(0, -2).Value = 0 And cell2.

我用If/Then创建了各种宏,但这次我遇到了If/Then进程的问题。在和之后,它不能正确执行If/Then的第二部分。它仅将之前和之后的偏移值转换为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
给你:

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

给你:

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函数是一个内置函数,被归类为逻辑函数,例如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

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