简体   繁体   English

VBA + 宏 + 对于每个单元格条件错误

[英]VBA + Macros + For each cell condition error

I would like to seek help with the codes below.我想就以下代码寻求帮助。

The first "IF" condition works well but the "ELSE" condition wherein I would like to skip the copy and paste function of the first condition and continue on to the loop does not function and gives me an error message that looks like this:第一个“IF”条件运行良好,但我想跳过第一个条件的复制和粘贴功能并继续循环的“ELSE”条件不起作用,并给我一条错误消息,如下所示: 在此处输入图片说明

Thank you in advance!先感谢您! more power to ya'll!给你们更多的力量!

Sub IF_Loop3()

    Dim cell As Range
    For Each cell In Range("TablePrac[Animals]")
        If cell.Value <> "NULL" Then
            cell.Offset(0, -3).Value = cell.Value
            
        Else
        If cell.Value = " " Then
        Exit For
        
        End If
        
    Next cell

End Sub
For Each cell In Range("TablePrac[Animals]")
        If cell.Value <> "NULL" Then
            cell.Offset(0, -3).Value = cell.Value
            
        Else If cell.Value = " " Then
             Exit For
             End If ' If cell.Value = " "
        End If 'If cell.Value <> "NULL" <= you forgot this one!
    Next cell

New code based on your explanation:基于您的解释的新代码:

Sub IF_Loop3()
    Dim cell As Range
    For Each cell In Range("TablePrac[Animals]")
        If (cell.Value <> "NULL") and (cell.Value <> " ") Then
            cell.Offset(0, -3).Value = cell.Value
        End If
    Next cell
End Sub

You have two conditions to execute the copy/paste: value <> "NULL" and value <> " " .您有两个条件来执行复制/粘贴: value <> "NULL"value <> " " You just need to put those two conditions in the If test for both to be respected.您只需要将这两个条件放在If测试中就可以了。

If you write Exit For it means the loop will break and your code will continue with the first line after the loop , not with the next iteration of the loop.如果您编写Exit For则意味着循环将中断并且您的代码将在循环之后的第一行继续,而不是循环的下一次迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM