If Cell value is <> 4 then delete Entire Row

NessPJ

Active Member
Joined
May 10, 2011
Messages
431
Office Version
  1. 365
Hi all,

I am having trouble getting the following cleanup code to work:
Code:
    Dim i As Integer

    For i = 2 To Range("A" & "65536").End(xlUp).Row Step 1       
        LResult = Len(Range("B" & i).Value)
        If LResult < 4 Or LResult > 4 Then
        Range("B" & i).EntireRow.Delete
        End If
    Next i

Column B has several values ranging from "0" to "999999999" and "AAAAAAAAA", but i want all rows deleted unless Cell B of that specific row contains a value between "1000" and "9999".

Am i doing something wrong? :)
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Ugh, i'm not allowed to edit my post a few mins after posting.

This line is also in the code:
Code:
Dim LResult As String
 
Upvote 0
Try changing your dim statement to
Code:
Dim LResult As long
As you are testing the length of the value it will return a number
 
Last edited:
Upvote 0
That still doesn't seem to cut it so it seems.

Code:
    Dim LResult As Long
    Dim i As Integer


    i = 0
    
    For i = 2 To Range("A" & "65536").End(xlUp).Row Step 1
         LResult = Len(Range("B" & i).Value)
        If LResult < 4 Or LResult > 4 Then
        Range("B" & i).EntireRow.Delete
        End If
    Next i
 
Last edited:
Upvote 0
When deleting (or inserting) rows it's always best to work bottom up. So try
Code:
 For i = Range("A" & "65536").End(xlUp).Row To 2 Step -1
If this doesn't resolve the problem, then please explain what is not working.
 
Upvote 0
When deleting (or inserting) rows it's always best to work bottom up. So try
Code:
 For i = Range("A" & "65536").End(xlUp).Row To 2 Step -1
If this doesn't resolve the problem, then please explain what is not working.


Thanks a lot, its working now! How odd this wont work with a normal search order through the range. Any ideas why perhaps? :)
 
Last edited:
Upvote 0
If you delete row 2, then row 3 moves up to become row 2 & the macro then checks row 3 (which was originally row 4).
So in this scenario the original row 3 will never get checked.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Okay, so i got around to actually testing this in practise.
Both tables have about 1200 entries and the code seems to take ages to execute (like...30 minutes).

Is there any way we would be able to speed this up?

[Edit]

Okay, so i added the following lines to the start and the end of the routine:
Code:
 Application.Calculation = xlManual

Code:
 Application.Calculation = xlAutomatic

This made all the difference in the world...the routine now finishes in less than 20 seconds. :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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