You are here:

Excel/Searching and deleting rows

Advertisement


Question
QUESTION: Hi Tom,

I have a VBA question for you please – excel 2016.

I have a lot of data in an excel sheet, only five columns at this stage, but the problem is it has approximately 1,000,000 rows od data. The big problem is it has multiple headers and blank rows spread throughout the rows, but always in a pattern that I will explain below.
I am not sure what is the best way to approach this in VBA, from the top down, or the bottom up, so have tried to explain it from both direction?

If I place the curser in cell “A8” and then press the SHIFT & Ctrl and the Down arrow it highlights down to the bottom of some data in column A (let’s say A91), then there are 2 blank cells and 6 cells with header data I want to delete. So 8 rows of data that I would like to delete (move cells up) so that the data becomes continuous, (in this case I would delete rows 92 to 99). Then I have do this many many times.

 
If I do it from the bottom up I would place the curser at the very bottom of column “A” where the data finishes, in this case “A1000000”, press the SHIFT & Ctrl and the Up arrow, it highlights up the column and stops at the last cell with data in it “A992498”, from this point I would delete the 2 empty rows above, “992496 to 992503” (8 rows in total). I would then continue this SHIFT & Ctrl and the Up arrow procedure until I reach row 8, then all the data is continuous and with only one header file that is above row 8.

Thank you for any help you can offer.

Regards
Natalie

ANSWER: Natalie,

based on your description, this worked for me:

Sub abc()
Dim rw As Long
rw = Cells(Rows.Count, "A").End(xlUp)
Do
rw = Cells(rw, "A").End(xlUp)
 If rw < 11 Then Exit Sub
 Cells(rw - 2, "A").Resize(8, 1).EntireRow.Delete
Loop While rw > 8
End Sub

test it on a copy of your worksheet until you are sure it does what you want.  

It worked for me as I understand how your data is laid out.  If I misunderstood, then it will probably not do what you want.  

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom,

Thanks for your prompt reply.

I have run your code and it come up with a "Type mismatch", I have stepped through the code and this seems to happen just after the "Do" part.

Do you have any idea what might be causing this?

Thanks once again for your help.

Regards
Natalie

Answer
Natalie,

Yes, there is a typo in the code:

Sub abc()
Dim rw As Long
rw = Cells(Rows.Count, "A").End(xlUp).Row   ' added .row
Do
rw = Cells(rw, "A").End(xlUp).Row          ' added .row
 If rw < 11 Then Exit Sub
 Cells(rw - 2, "A").Resize(8, 1).EntireRow.Delete
Loop While rw > 8
End Sub

I am scratching my head on this one because it shouldn't have worked without the .row, but it tested fine.  It does work with the .row and that should solve your problem.  Ah - now I know why - to make a set of test data I put the row number in column A so that allowed it to work.   Just a bad coincidence that caused me not to notice the omission.

Anyway, I altered my test data to strings and the new code works fine.

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.