Can anyone speed this macro up?

justanotheruser

Board Regular
Joined
Aug 14, 2010
Messages
96
Hi guys,

I've created this macro that will search down the rows in a sheet and delete all rows from A2 downwards if it has a "#" in Column A. However, each time it is run, it is looking down 10,000 rows - so it is taking a very long time to run!

Code:
Sub DeleteRowWithContents1()
'========================================================================
' DELETES ALL ROWS FROM A2 DOWNWARDS WITH THE WORDs "#"IN COLUMN A
'========================================================================
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "A").Value) = "#" Then
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
End Sub

Is there any way to make this work faster? One idea I had was to find all the rows that don't have a # in Column A (a "#" will be the only value of the cell if the row is to be deleted) and copy and paste those into a new sheet - is this possible, as I'd imagine it'd be a lot faster?



Excel Workbook
ABC
1scenario_codecompany_codeaccount_code
2###
3###
4###
5###
6###
7###
8###
9###
10###
11###
122011_ACTSSGUK100100
132011_ACTSSGUK100100
142011_ACTSSGUK100100
152011_ACTSSGUK100100
162011_ACTSSGUK100100
17###
18###
19###
ETL ICO


Thanks in advance for your help! :)
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Add this to your code

Code:
Sub macroname()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' Your code here...

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I think you'll find AutoFiltering a lot faster. Filter to show the rows with "#" and then delete the rows in one go.

Code:
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    Range("A1").AutoFilter
    ActiveSheet.Range("$A$1:$A$" & Last).AutoFilter Field:=1, Criteria1:="#"
    Rows("1:" & Last).Offset(1).Select
    Selection.Delete Shift:=xlUp
    Selection.AutoFilter
 
Last edited:
Upvote 0
Rather than testing each row, record a macro where you filter column A for "#", and then delete visible cells only.
 
Upvote 0
I think you'll find AutoFiltering a lot faster. Filter to show the rows with "#" and then delete the rows in one go.



Also, if the order of rows doesn't matter then you can sort by column A first, so that all the # rows are next to each other. Excel can sort, then delete one block a lot faster than it can delete a whole bunch of non-contiguous rows...
 
Upvote 0
Hi all! Thank you so much for your extremely fast responses! To reply to you taigovinda, unfortunately the order of the code does matter so I am unable to sort them as you suggested.

Is there a code that will delete the visible cells only, I don't know quite how to do that:

This is my code that creates the auto filter filtering out everything except the #s:
Code:
Rows("1:1").Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$J$10000").AutoFilter Field:=1, Criteria1:="#"
    ActiveSheet.EntireRow.Delete

Now how do I run down the rows and delete them al (as they are all #s) l? :)
 
Last edited:
Upvote 0
Have a look at SpecialCells(xlCellTypeVisible). That can be used to delete only the visible cells.
 
Upvote 0
This uses the autofilter to only show the #'s, and then deletes the visible cells.

Selection.AutoFilter Field:=1, Criteria1:="#"
Cells.Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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