Making a Row Delete Macro run better

Jnrrpg11

New Member
Joined
Jun 29, 2017
Messages
28
Hello Everyone,

I should start by saying I've only a very basic grasp of VBA.

Some of the guys on this forum have helped me with some VBA code to delete unwanted rows of data from a spreadsheet, which does exactly what it should so thanks for that.

Only thing is that i've run it with the same source data over and over in testing it may take 3 minutes to process or 10 minutes. I can't understand what would cause it to be so drastically different.

The code is here -

Sub MainRoutineA()


Dim ws As Worksheet
Dim Rng As Range
Dim lastRow As Long


Application.ScreenUpdating = False


Set ws = ActiveWorkbook.Sheets("FY Budget from ART")


lastRow = ws.Range("I" & ws.rows.Count).End(xlUp).Row




Set Rng = ws.Range("I5:I" & lastRow)


With Rng
.AutoFilter Field:=1, Criteria1:="<>" & Worksheets("User Information").Range("I34").Value
.SpecialCells(xlCellTypeVisible).EntireRow.Delete

EndWith


ws.AutoFilterMode = False


Application.ScreenUpdating = True




EndSub



In the workbook, a user will select their name from a dropdown on "User Information" - "I34".
The Code will look down column I and remove any instances where that users name doesn't exist.

Is there any tips for using this type of code that would potentially speed it up or can you see any red flags that cause known speed issues in the above code?
If it helps the source data will average about 11,000 rows and once run cut the data to maybe 250-750 Rows.


As always in advance.

Richard
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
On 12000 rows it only takes 24 secs
Do you have any event code in your sheet?
Also do you have lots of formulae?
 
Upvote 0
Hey Fluff,

Thanks for replying.
The answer to your question is yes, there's lots of formula in the workbook. Is there a way of halting all calculations until the macro runs. I've used DoEvents before but I'm not sure if it's right in this case or where to put it or if there's a better option. There's also a lot of event code but not relating to this sheet.

Thanks again.
 
Upvote 0
You've already turned screenupdating of/on so add the calculation
Code:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
If its still taking a long time, let me know.
 
Upvote 0
Code:
Application.Calculation = xlManual
& then back to automatic....
Code:
Application.Calculation = xlAutomatic
 
Upvote 0
So the application calculation is always off. I use code 'this workbook' to ensure that.

Sub Workbook_Open()


Application.Calculation = xlManual
Application.CalculateBeforeSave = False




End Sub




Sub Workbook_BeforeClose(Cancel As Boolean)


Application.Calculation = xlAutomatic
Application.CalculateBeforeSave = True




End Sub

Once the user pastes their information in and clicks calculate the sheet runs the macros to thin out the source data.
Once this takes place it calls a macro that manually calculates the sheets (which look more like dashboards).

Interestingly, i remember using this macro before and it ran really quickly so I'm not sure why it so slow now.
 
Upvote 0
If you have Conditional Formatting applied, you might consider deleting it to see if it's impacting performance.

Based on one project experience, CF can also be a memory hog, increasing in size as more iterations of the macro are run, and slowing execution with each iteration.

Cheers,

tonyyy
 
Upvote 0
.. it may take 3 minutes to process or 10 minutes. I can't understand what would cause it to be so drastically different.

..can you see any red flags that cause known speed issues in the above code?
Apart from things already mentioned in the thread, what can cause significant differences in run-time for such a macro is the number of disjoint ranges that need to be deleted. For example, consider the sheet below where the aim is to delete the green rows from each table. Both tables have 6 green rows but Table 1 only has 2 ranges to delete while Table 2 has 6 ranges to delete. The deletion for Table 2 will be slower than for Table 1.


Book1
A
1Table 1
2a
3b
4c
5d
6e
7f
8g
9h
10i
11j
12k
13
14
15Table 2
16a
17b
18c
19d
20e
21f
22g
23h
24i
25j
26k
Sheet2



Is there any tips for using this type of code that would potentially speed it up ..
Yes - use the above information to gather all the deletion rows together so that there is only one section of rows to delete.

I haven't set the Calculation mode in my code as you have said that has already been taken care of. Give this a try in a copy of your workbook but also check my note below the code.

Rich (BB code):
Sub Del_Rows()
  Dim a As Variant, b As Variant
  Dim CompVal As String
  Dim nc As Long, i As Long, k As Long, lastrow As Long
  
  CompVal = ActiveWorkbook.Worksheets("User Information").Range("I34").Value
  With ActiveWorkbook.Sheets("FY Budget from ART")
    nc = .Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
    lastrow = .Range("I" & .Rows.Count).End(xlUp).Row
    With .Rows("6:" & lastrow)
      a = .Columns(9).Value
      ReDim b(1 To UBound(a), 1 To 1)
      For i = 1 To UBound(a)
        If a(i, 1) <> CompVal Then
          b(i, 1) = 1
          k = k + 1
        End If
      Next i
      If k > 0 Then
        Application.ScreenUpdating = False
          .Columns(nc).Value = b
          .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
          .Resize(k).EntireRow.Delete
        Application.ScreenUpdating = True
      End If
    End With
  End With
End Sub

Note:
The code is case-sensitive so if 'User Information' cell I34 could be "Fred" but column I of the deletion sheet could contain "fred", "FRED" etc then change these two lines in the code.
Rich (BB code):
CompVal = LCase(ActiveWorkbook.Worksheets("User Information").Range("I34").Value)

If LCase(a(i, 1)) <> CompVal Then
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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