简体   繁体   中英

Loop not going to next element

I am trying to set every cell in Column C to Blank if its cells match the value in column B

This is my code

For Each b In Columns("B").Cells
    For Each c In Columns("C").Cells
        If b.Value = c.Value Then
        c.Value = Null
    Next c
Next b

b and c are both declared as ranges.

The error I get is "Next without for".

Any idea why?

this is a better approach i think....

dim rNg as range
dim rCell as range

set rNg = ThisWorkbook.Sheets("Sheet1").Range("your range") 'im assuming B:B in my code

for each rCell in rNg.Cells
    if rCell.Value <> vbNullString And rCell.Offest(0,1).Value <> vbNullString then
        if rCell.Value = rCell.Offset(0,1).Value Then
            rCell.Offset(0,1).Value = vbNullString
        End If
    End If
next rCell

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