VBA Q

cfree36

Board Regular
Joined
Oct 5, 2005
Messages
175
I am trying to delete all the rows with a 1 in a specified range

I was trying to use this code:
Code:
'    For Each c In Sheets("sheet1").Range("FILTERRANGE")
'        If c.Value = 1 Then
'        c.Select
'        Selection.EntireRow.Delete
'        End If
'    Next c

It runs but the rows with 1 in them do not delete?

I would appreciate any help here....
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
Code:
If c.Value = "1" Then
could help if the values are text.
Another option is use val(c.value)to convert it to an integer.

Note also, that you are running your loop from top to bottom, which means that if any rows are actually deleted, it skips the row after every row deleted.
 

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
I tried and it works for me? Is there a named range area setup "FILTERRANGE"? Also is the 1 in a cell by itself? Because that is what this code is looking for
 

cfree36

Board Regular
Joined
Oct 5, 2005
Messages
175
there is a range named filterrange, each cell has either a 0 or a 1 based on an IF formula.

I very well could be skipping rows... how do I make the code run from bottom to top?
 

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
Code:
    Dim i As Integer, j As Integer
    
    With Sheets("Sheet1").Range("test")
    For i = .Rows.Count - 1 To 0 Step -1
        For j = .Columns.Count - 1 To 0 Step -1
            If Val(Cells(.Row, .Column).Offset(i, j).Value) = 1 Then
                Cells(.Row, .Column).Offset(i, j).EntireRow.Delete
            End If
        Next j
    Next i
    End With
 

cfree36

Board Regular
Joined
Oct 5, 2005
Messages
175
THANKS.
When I get a second I'll give that a try as well.
Sorry for the delayed thanks... it's been crazy at work this week!

THANKS AGAIN!
 

Forum statistics

Threads
1,141,587
Messages
5,707,236
Members
421,498
Latest member
matinebi

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
Top