Multiple selections

dedelkoo

New Member
Joined
Mar 19, 2010
Messages
28
Hi All:

I'm trying to code a macro to delete irrelevant data and the for loop I'm using takes considerable time; I'm wondering if selecting all the irrelevant rows and then deleting would be faster than deleting each row as its found.

Currently the code I have is:

For x = LR To 2 Step -1
If Range("B" & x).Value = "" Then
Rows(x).Select
Selection.Delete shift:=xlUp
End If
Next x
(LR is a line count variable)
This code works but is slow as the sheet usually has in excess of 10,000 rows. I'm hoping there's syntax I could use in the loop to keep adding to my selected rows; then once the for loop is done have one line that deletes the entire selection. Any thoughts?

Many thanks.
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
I have no idea but an easy way to make your code faster is by using
Application.ScreenUpdating

So your code would be
Code:
Application.ScreenUpdating = False
For x = LR To 2 Step -1
If Range("B" & x).Value = "" Then
Rows(x).Select
Selection.Delete shift:=xlUp
End If
Next x
Application.ScreenUpdating = True
 

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Moreover, it would be a good idea if you removed the .Select function.
It slows down your code alot.

So instead of
Code:
Rows(x).Select
Selection.Delete shift:=xlUp
Use
Code:
Rows(x).Delete shift:=xlUp
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,731
Messages
5,513,074
Members
408,936
Latest member
savageanman

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top