ClearContents of the row if finds all 1's in it.

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Hi,</SPAN></SPAN>

Sheet1 show the data in the columns C:P are filled with 1's in it. But the row 6, 7, 8, 9 has in it X or 2 filled, is it possible ClearContents (do not eliminate the rows) of the row in which has only 1's in it (for example row 10 to 22 row) </SPAN></SPAN>

Note: filled row would be in order up to down there will not be any row in middle filled with X's or 2's</SPAN></SPAN>

Book1
ABCDEFGHIJKLMNOPQR
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14
611111X11111111
721111111111111
81X111111211111
9111XXX12111111
1011111111111111
1111111111111111
1211111111111111
1311111111111111
1411111111111111
1511111111111111
1611111111111111
1711111111111111
1811111111111111
1911111111111111
2011111111111111
2111111111111111
2211111111111111
23
24
25
Sheet1


And after macro get the result as shown below only rows filled with mixed 1, X, 2</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQR
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14
611111X11111111
721111111111111
81X111111211111
9111XXX12111111
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Sheet1-Result


Thank you in advance</SPAN></SPAN>

Using Excel 2000</SPAN></SPAN>

Regards,</SPAN></SPAN>
Kishan</SPAN>
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If I understand correctly, once you find a row that is only 1s you want to clear that row & all rows below.
Code:
Sub Kishan()
   Dim Cl As Range
   Dim UsdRws As Long
   
   UsdRws = Range("C" & Rows.Count).End(xlUp).Row
   For Each Cl In Range("C6:C" & UsdRws)
      If Application.CountIf(Cl.Resize(, 14), 1) = 14 Then
         Cl.Resize(UsdRws - Cl.Row + 1, 14).Clear
         Exit For
      End If
   Next Cl
End Sub
 
Upvote 0
Try this:
Code:
Sub ClearRows()

    Dim lr As Long, r As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
'   Find last row in column C with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Loop through all rows starting in row 6
    For r = 6 To lr
'       Check to see if equal to 1 in all columns
        Set rng = Range(Cells(r, "C"), Cells(r, "P"))
        If Application.WorksheetFunction.CountIf(rng, 1) = 14 Then
'           Clear range
            rng.ClearContents
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
If I understand correctly, once you find a row that is only 1s you want to clear that row & all rows below.
Code:
Sub Kishan()
   Dim Cl As Range
   Dim UsdRws As Long
   
   UsdRws = Range("C" & Rows.Count).End(xlUp).Row
   For Each Cl In Range("C6:C" & UsdRws)
      If Application.CountIf(Cl.Resize(, 14), 1) = 14 Then
         Cl.Resize(UsdRws - Cl.Row + 1, 14).Clear
         Exit For
      End If
   Next Cl
End Sub
Fluff, it worked perfect as I request I did changed .Clear to .ClearContents so not to delete the format.</SPAN></SPAN>

Thank you for your help
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Try this:
Code:
Sub ClearRows()

    Dim lr As Long, r As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
'   Find last row in column C with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Loop through all rows starting in row 6
    For r = 6 To lr
'       Check to see if equal to 1 in all columns
        Set rng = Range(Cells(r, "C"), Cells(r, "P"))
        If Application.WorksheetFunction.CountIf(rng, 1) = 14 Then
'           Clear range
            rng.ClearContents
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
Joe4, I liked your solution too. Which lives all rows that contains X and 2 and ClearContents rest rows that is only 1's </SPAN></SPAN>

Thank you for your help
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Glad we could help& thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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