Sorting and removal of data

AnnaKarin

New Member
Joined
Aug 17, 2011
Messages
7
Hi!
I have a big spreadsheet with data. For each column I need to remove cells with numbers that are below 0 and above 2. As I do it now I use filters to find the numbers easily and delete them manually, but there must be some more efficient way? And there are a lot of columns to go through...
 

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
What columns do you need this to happen?

Are these the interger values 0, 1, 2 or also values in between?

Do you need to clear the cells or delete the row?

Do the cells contain formulas or hard values?
 
Upvote 0
I need it to happen for all columns.
I need to save all values between 0-2.
The cells should be deleted, not the column.
The columns only contain hard values, no formulas.
 
Upvote 0
Then, consider this code:

Code:
Dim rng As Range
For Each rng In ActiveSheet.UsedRange
   If rng.Value >= 0 And rng.Value <= 2 Then rng.ClearContents
Next

Depending on what else happens in your sheet, restricting the UsedRange using SpecialCells might speeding up macro executing.
 
Last edited:
Upvote 0
A possibly much faster approach would be:

Code:
Sub ff()

    Dim rng As Range
    
    With ActiveSheet.UsedRange
    
        sq = .Value
    
        For i = 1 To UBound(sq, 1)
            For j = 1 To UBound(sq, 2)
                If sq(i, j) >= 0 And sq(i, j) <= 2 Then sq(i, j) = ""
            Next
        Next
    
        .Value = sq
        
    End With

End Sub
, or variants of this code.
 
Upvote 0
Thank you for your answers! Unfortunately I'm a bit of a beginner at this so I have to read up on how to make macros :eeek: But you got to learn sometime right ;)
 
Upvote 0
It works!! :)
Although the
">= 0 And rng.Value <= 2"
should be
"<= 0 And rng.Value >= 2"

Thank you so much for your help!
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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