For Loop Taking Too Long

Peter.Stevens2

Board Regular
Joined
Sep 16, 2008
Messages
56
Hi, I've got a sheet which I need to analyse and split into several different sheets but the raw data format leaves a lot to be desired as there are blank rows inserted randomly between rows of data. I need the 'good' data to stay in the same order so I've written a macro to sort through the data and delete any blank rows leaving the good stuff behind. The problem is this takes ages as there can be up to 30000 rows that need to be checked and I need to do this 5-6 times a day. I just wondered weather there was a quicker way to do this? The code I've got is detailed below:

Code:
Sub Prep2()
'Delete all blank data rows
Dim Rows As Double
Dim Rownum As Double
Application.ScreenUpdating = False
Rows = Selection.SpecialCells(xlLastCell).Row
 
For Rownum = 2 To Rows
    If Cells(Rownum, 11) <> "" Then GoTo NxtRownum Else
 
    Cells(Rownum, 11).EntireRow.Delete shift:=xlUp
 
    Rows = Rows - 1
NxtRownum:
Next Rownum
Application.ScreenUpdating = True
End Sub

Many Thanks for any help you can offer!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try

Code:
Sub Prep2()
On Error Resume Next
Columns("K").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
End Sub
 
Upvote 0
Thanks for the quick reply, the macro deletes all of the data on the sheet when I run it. Should I be looking at a specific cell rather than the whole column?
 
Upvote 0
It is looking for blanks in column K which is what I believe your code was doing. If a blank is found in that column the whole row is deleted. Is that what you want?
 
Upvote 0
Hi, That's exactly what I want to do and I can see your code would do that very quickly but when I run it I end up with a blank sheet where the raw data used to be. :confused: Could it be something to do with the format of the raw data, it's a Tab delimited file and I've had problems with this type of file before?
 
Upvote 0
Maybe it is something about the structure of the file. See if this amendment to your original loop is any faster

Code:
Sub Prep2()
'Delete all blank data rows
Dim LR As Long, Rownum As Long
Application.ScreenUpdating = False
LR = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
For Rownum = LR To 2 Step -1
    If Cells(Rownum, 11).Value = "" Then Rows(Rownum).Delete
Next Rownum
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi

Give this a shot (may not do anything different from Peter's though):

Code:
Sub DeleteRows()
Dim r As Range
Rows(1).Insert
[K1] = "Header"
Set r = Application.Intersect(ActiveSheet.UsedRange,Range("K:K"))
r.Autofilter Field:=1,Criteria1:="="
r.EntireRow.Delete
End Sub
 
Upvote 0
Hi,

Thanks VoG that ammendment was about 10 times quicker than mine, I think thats about as quick as I'm going to get it with the file issues I'm having, I think there is something about the way the raw data is constructed (downloaded from SAP) that makes it difficult to manipulate in Excel. 25 seconds to sort nearly 30000 records is much better than the 5 minutes I was waiting before, I really appreciate the help, I've been scratching my head over this one!

Kind Regards

Pete
 
Upvote 0
Well that's an improvement but I would try Richard's code - it should be faster.
 
Upvote 0
Based on this website, http://www.aivosto.com/vbtips/stringopt.html, we can enhance Vog's code a little more by using Lenb:

Code:
Sub Prep2()
'Delete all blank data rows
Dim LR As Long, Rownum As Long
Application.ScreenUpdating = False
LR = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
For Rownum = LR To 2 Step -1
    If lenb(Cells(Rownum, 11).Value) = 0 Then Rows(Rownum).Delete
Next Rownum
Application.ScreenUpdating = True
End Sub</pre>
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,852
Members
449,194
Latest member
HellScout

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