VBA code takes ~1 min to process simple macro

ExcelSOS21

New Member
Joined
Sep 29, 2021
Messages
1
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. MacOS
Hello,
I have simple macro (see below) that works but it takes an unusually long time to process large data sets.
The macro runs between two worksheets within the same workbook.
In the main worksheet (Sheet3), the User selects a "taskname" (Sheet 3 cell d2) and then kicks off the macro to search (Sheet1) for all matches.
The macro then copies and paste the matches from Sheet1 to Sheet3.

I would appreciate any recommendations for cutting down processing time for large data sets.

I think I may need to use array, but I am not sure how to.

Thank you in advance for your help.



Sub finddata()

'1. declare variables

Dim taskname As String
Dim finalrow As Integer
Dim i As Integer


'2. clear old search results

Sheet3.Range("B6:J40").ClearContents


'3. find records that match criteria and paste them in Sheet3

taskname = Sheet3.Range("d2").Value
finalrow = Sheet1.Range("A1000").End(xlUp).Row



For i = 2 To finalrow

If Sheet1.Cells(i, 1) Like taskname Then

Sheet1.Activate

ActiveSheet.Range(Cells(i, 2), Cells(i, 10)).Copy

Sheet3.Range("B100").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats

End If

Next i


Application.Goto (ActiveWorkbook.Sheets("Sheet3").Range("d2"))

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Any quicker
VBA Code:
Sub finddata()
Dim taskname As String, finalrow As long, i As long
Application.ScreenUpdating = False
Sheet3.Range("B6:J40").ClearContents
taskname = Sheet3.Range("d2").Value
finalrow = Sheet1.Range("A1000").End(xlUp).Row
For i = 2 To finalrow
If Sheet1.Cells(i, 1) Like taskname Then
Sheet1.Range(Cells(i, 2), Cells(i, 10)).Copy
Sheet3.Range("B100").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next i
Application.Goto (Sheets("Sheet3").Range("d2"))
Application.ScreenUpdating = True
End Sub
 
Upvote 0
What about doing them all at once? As far as i can see this does the same as your code.

BTW, when posting vba code, please indent your code and use the code tags provided to preserve the formatting. Makes helping much easier. :)
My signature block below has more information.

VBA Code:
Sub finddata_v2()
  Application.ScreenUpdating = False
  Sheet3.Range("B6:J40").ClearContents
  With Sheet1.Range("A1:I" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row)
      .AutoFilter Field:=1, Criteria1:=Sheet3.Range("D2").Value
      .Offset(1, 1).Resize(.Rows.Count - 1).Copy
      Sheet3.Range("B6").PasteSpecial xlPasteFormulasAndNumberFormats
  End With
  Sheet1.AutoFilterMode = False
  Application.CutCopyMode = False
  Application.Goto (ActiveWorkbook.Sheets("Sheet3").Range("d2"))
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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