IF OR with LEN

Babio1964

New Member
Joined
May 12, 2015
Messages
8
Hello,
This on has me baffled and I tried everything I could think of. Longstory short, I'm working with an old windows program that extracts data into anexcel worksheet. There are a ton of rows,but many are useless (like headers, totals, other stuff) Anyway, I did discover that the data I needhad a consistent length of 188 characters, so I wrote a little sub that findsall the data not 188 characters and removes all those rows. This worked ideally, until I later discoveredthere were a few outliers. Fortunately,these outliers are also consistently 123 characters. Currently, this is the code IÂ’m using for the first one isbelow.


Range("A1").Select

For Counter = 1 ToLastRow

IfLen(ActiveCell.Value) <> 188 Then
ActiveCell.EntireRow.Delete ' Delete Row

Else
ActiveCell.Offset(1,0).Select ' Move down a row

End If
Next Counter

My problem is that when I try to incorporate anadditional condition (also keeping the 123 characters) everything is deleted. In short, I need to keep all the LEN = 188 aswell as the LEN = 123 rows, but remove everything else. I tried: If Len(ActiveCell.Value)<> 188 OR Len(ActiveCell.Value) <> 123 Then.. to no avail

I’d covet any help

Thank you!
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi, you need to use AND not OR..

Also, when deleting rows you should loop from the bottom upwards, for example:

Code:
For Counter = LastRow To 1 Step -1


    If Len(Cells(Counter, "A").Value) <> 188 And Len(Cells(Counter, "A").Value) <> 123 Then
        Cells(Counter, "A").EntireRow.Delete ' Delete Row
    End If


Next Counter
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top