Maxx Sawyer
Maxx Sawyer

Reputation: 1

Clearing contents of cell with for loop and if statement

I have this bit of code:

For Each cell In Worksheets("Sheet1").UsedRange
      If(Left(cell, 3) <> 574 Or Left(cell, 3) <> 584 Or Left(cell, 3) <> 233) Then cell.ClearContents
Next

The codes purpuse is to loop through all used cells in the spread sheet and if the first three digits are not 574, 233, 584, then the cell will be set to empty.

In my spreadsheet I have values that should pass, the first three digits are equal to 574, 233, 584; but instead the loop delete everything from the spreadsheet. Any insight into errors in logic I may have would be welcomed.

Upvotes: 0

Views: 34

Answers (1)

Scott Holtzman
Scott Holtzman

Reputation: 27249

Whenever I see multiple conditions for IF statement, I immediately think about using Select Case for ease of readability and maintenance.

For Each cell In Worksheets("Sheet1").UsedRange

    Select Case Left(cell,3)

        Case Is = 574, 584, 233 'do nothing
        Case Else: cell.ClearContents

    End Select

Next

Upvotes: 3

Related Questions