Delete rows depending on number of columns

gildartzz

New Member
Joined
Apr 28, 2021
Messages
7
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Hi, I would like to know how one can delete multiple rows depending on the number of columns that they have.
For eg. say I have 100 rows and the number of columns with data are different for each row. Now I only want rows that have exactly 10 columns with data and delete the ones that dont meet this requirement.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
What about this? I only looked at columns A to Z...change as needed.

Code:
Sub Keep10()
Dim lr As Long, i As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = lr To 2 Step -1
If WorksheetFunction.CountA(Range("A" & i & ":Z" & i)) <> 10 Then
Rows(i).EntireRow.Delete
Else
End If
Next i
End Sub
 
Upvote 0
Solution
I think the line calculating lr should be changed to this:

Code:
lr = ActiveSheet.UsedRange.Rows.Count

I made a bad assumption.
 
Upvote 0
Hey kweaver. thanks for your response before. I tried running the code you posted but the result was that all data except the first row was removed.
 
Upvote 0
Hey kweaver. thanks for your response before. I tried running the code you posted but the result was that all data except the first row was removed.
this image is an example of the data I have. there over 500000 rows in the actual dataset. I want to keep only those rows that have exactly 10 entries, and delete the rest.
 

Attachments

  • Screenshot 2021-04-28 165526.png
    Screenshot 2021-04-28 165526.png
    38.8 KB · Views: 15
Upvote 0
Did you change the routine in any way? It has worked for me with dozens of rows and hundreds of rows.
 
Upvote 0
not much. just changed A:Z to B:S
Also when I ran it the first time it kept running for close to 3 hrs. Then I tried it running it on a smaller sample and the result was what I mentioned above.
 
Upvote 0
I think all of the rows would be deleted in your example because from B to S there are always more than 10 data values.

Also, please install XL2BB so that your data can be easily copied rather than recreating.

I just ran it with 1000 rows (plus the header) and it took seconds and seems to work just fine keeping only the rows with 10 entries from B to S.
 
Upvote 0
Another way to do this would be using filter but I haven't tried that. Might be faster.

On my PC it took about 15 seconds to run with 2000 rows.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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