minimize run time for macro

Lenna

Active Member
Joined
Jun 25, 2014
Messages
269
I have an operation that takes too long. Please suggest how to make it run faster.


Code:
Option Compare Text


Sub Takes2Long()


Application.ScreenUpdating = False


Dim cel As Range, rng As Range, lr As Long
Dim cel1 As Range, rng1 As Range, lr1 As Long
'deletecellsunderneath
lr = Worksheets("Sheet1 (2)").Cells(Rows.Count, "Q").End(xlUp).Row
Set rng = Worksheets("Sheet1 (2)").Range("Q2:Q" & lr) 'assumes a header row
    For Each cel In rng
        If InStr(cel.Value, "donor") > 0 Or InStr(cel.Value, "transplant") > 0 Then
          lr2 = Worksheets("DSA").Cells(Rows.Count, "Q").End(xlUp).Row 'sheet being pasted to, change if needed
           cel.Offset(1, 0).EntireRow.Cut Destination:=Worksheets("DSA").Range("A" & lr2 + 1)
        End If
   Next cel
  'cutdonorandtrasnplant
   lr1 = Worksheets("Sheet1 (2)").Cells(Rows.Count, "Q").End(xlUp).Row
Set rng1 = Worksheets("Sheet1 (2)").Range("Q2:Q" & lr1) 'assumes a header row
    For Each cel1 In rng1
        If InStr(cel1.Value, "donor") > 0 Or InStr(cel1.Value, "transplant") > 0 Then
           lr3 = Worksheets("DSA").Cells(Rows.Count, "Q").End(xlUp).Row 'sheet being pasted to, change if needed
           cel1.Offset(0, 0).EntireRow.Cut Destination:=Worksheets("DSA").Range("A" & lr3 + 1)
        End If
   Next cel1
  
 Application.ScreenUpdating = True


 End Sub
Thanks,

Lenna
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Lenna

Active Member
Joined
Jun 25, 2014
Messages
269
I'm cutting and pasting all rows underneath a row that contains "donor" or "transplant" in column Q. The second part of the macro cuts and pastes all rows that contain "donor" and "transplant". I'm sure the two loops can be combined but I'm not sure how.

I need to remove a row underneath first and a row that contains second.

Thanks,

Lenna
 
Upvote 0

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,964
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello

Why don't you cut 2 areas at once? This will change the order of the rows, though.

Code:
Option Compare Text

Sub Takes2Long()

    Dim cel As Range, lr As Long, lr2 As Long
    
    Application.ScreenUpdating = False
    
    'deletecellsunderneath
    lr2 = Worksheets("DSA").Cells(Rows.Count, "Q").End(xlUp).Row
    lr = Worksheets("Sheet1 (2)").Cells(Rows.Count, "Q").End(xlUp).Row
    For Each cel In Worksheets("Sheet1 (2)").Range("Q2:Q" & lr)
        If InStr(cel.Value, "donor") > 0 Or InStr(cel.Value, "transplant") > 0 Then
            cel.Offset(1).Resize(2).EntireRow.Cut Destination:=Worksheets("DSA").Range("A" & lr2 + 1)
            lr2 = lr2 + 2
        End If
    Next

    Application.ScreenUpdating = True

End Sub

Do not cut entire rows, limit to the number of columns needed:

Code:
cel.Offset(1).Resize(2).EntireRow.Cut

could become for example:

Code:
cel.Offset(1, 1 - cel.Column).Resize(2,26).Cut

if you need to cut columns A to Z (which is 26 columns starting in column A).
 
Upvote 0

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,964
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Without changing the order of pasting:

Code:
Option Compare Text

Sub Takes2Long()


    Dim cel As Range, lr As Long, lr2 As Long
    
    Application.ScreenUpdating = False
    
    lr2 = Worksheets("DSA").Cells(Rows.Count, "Q").End(xlUp).Row
    lr = Worksheets("Sheet1 (2)").Cells(Rows.Count, "Q").End(xlUp).Row
    For Each cel In Worksheets("Sheet1 (2)").Range("Q2:Q" & lr)
        If InStr(cel.Value, "donor") > 0 Or InStr(cel.Value, "transplant") > 0 Then
            cel.Offset(1).EntireRow.Cut Destination:=Worksheets("DSA").Range("A" & lr2 + 1)
            cel.EntireRow.Cut Destination:=Worksheets("DSA").Range("A" & lr2 + 2)
            lr2 = lr2 + 2
        End If
    Next


    Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,191,686
Messages
5,988,073
Members
440,125
Latest member
vincentchu2369

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
Top