Trying to make a macro more efficient

SDMaestro

New Member
Joined
Dec 7, 2017
Messages
10
Hi All !

I have a piece of code written from the macro recorder which serves its purpose but runs quite slow as the data set increases, my VBA skills are limited so I'm wondering if or how the code below could be written to make the process more efficient ?

Basically what it will do is copy a list of filtered jobs (by type) from a table, then paste the jobs in a new sheet/table (Services), and remove any duplicate job numbers. The macro will run through and do the same process for a different job type and paste into another sheet/table (Rental)

Any help is much appreciated.

Code:
[I]Sub Refresh()[/I]
[I]'[/I]
[I]' Refresh Macro[/I]

[I]    Sheets("Monthly tables").Select[/I]
[I]    ActiveSheet.ListObjects("YTD").Range.AutoFilter Field:=8, Criteria1:= _[/I]
[I]        "Services"[/I]
[I]    Range("YTD[Job" & Chr(10) & "Number]").Select[/I]
[I]    Selection.SpecialCells(xlCellTypeVisible).Select[/I]
[I]    Selection.Copy[/I]
[I]    Sheets("Services").Select[/I]
[I]    Range("Services[Job Number]").Select[/I]
[I]    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _[/I]
[I]        :=False, Transpose:=False[/I]
[I]    Application.CutCopyMode = False[/I]
[I]    ActiveSheet.Range("Services[Job Number]").RemoveDuplicates Columns:=1, Header:= _[/I]
[I]xlYes[/I]

[I]    Sheets("Rental").Select[/I]
[I]    Sheets("Monthly tables").Select[/I]
[I]    ActiveSheet.ListObjects("YTD").Range.AutoFilter Field:=8, Criteria1:= _[/I]
[I]        "Rental"[/I]
[I]    Range("YTD[Job" & Chr(10) & "Number]").Select[/I]
[I]    Selection.SpecialCells(xlCellTypeVisible).Select[/I]
[I]    Selection.Copy[/I]
[I]    Sheets("Rental").Select[/I]
[I]    Range("Rental[Job Number]").Select[/I]
[I]    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _[/I]
[I]        :=False, Transpose:=False[/I]
[I]    Application.CutCopyMode = False[/I]
[I]    ActiveSheet.Range("Rental[Job Number]").RemoveDuplicates Columns:=1, Header:= _[/I]
[I]xlYes[/I]


[I]End Sub[/I]
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This about as much as I could do just looking at the code. Seeing your file might provide insights for a more efficient design.

Code:
Sub Refresh()
'
' Refresh Macro


   Application.ScreenUpdating = False
   
   With Sheets("Monthly tables")
      .ListObjects("YTD").Range.AutoFilter Field:=8, _
                                           Criteria1:="Services"
      .Range("YTD[Job" & Chr(10) & "Number]").SpecialCells(xlCellTypeVisible).Copy
      Sheets("Services").Range("Services[Job Number]").PasteSpecial Paste:=xlPasteValues, _
                                                                    Operation:=xlNone, _
                                                                    SkipBlanks:=False, _
                                                                    Transpose:=False
      Application.CutCopyMode = False
      .Range("Services[Job Number]").RemoveDuplicates Columns:=1, _
                                                      Header:=xlYes
   End With
   
   With Sheets("Monthly tables")
      .ListObjects("YTD").Range.AutoFilter Field:=8, _
                                           Criteria1:="Rental"
      .Range("YTD[Job" & Chr(10) & "Number]").SpecialCells(xlCellTypeVisible).Copy
      Sheets("Rental").Range("Rental[Job Number]").PasteSpecial Paste:=xlPasteValues, _
                                                                Operation:=xlNone, _
                                                                SkipBlanks:=False, _
                                                                Transpose:=False
      Application.CutCopyMode = False
      .Range("Rental[Job Number]").RemoveDuplicates Columns:=1, Header:= _
      xlYes
   End With
   
   Application.ScreenUpdating = True




End Sub
You might want to review my write-up on macro recorder bloat.
 
Last edited:
Upvote 0
Thanks 6string ! I'll give this a go tomorrow & see if I can attach a template that might be easier to follow.
 
Upvote 0
The code you provided does seem to eliminate some waste from the macro recorder, appreciate it.

The link below is a skeleton of the template if it helps better illustrate what I am asking. Typically the job list would be much longer.

http://ge.tt/5gwXQqt2
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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