简体   繁体   中英

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: 在此处输入图片说明

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 <> " " . You just need to put those two conditions in the If test for both to be respected.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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