Need help replacing For Next Loop with AutoFilter

bubbapost

Board Regular
Joined
Mar 11, 2009
Messages
116
Hello,

I have a Time Off Reviewer workbook that is basically UI with a workbook with all the data on the backend. I starting out using the For/Next loop, but as the data grew, it kept getting slower and slower. Can someone help me speed up this code with AutoFilter?

Here is my code:

Code:
Sub Approved()
Set WBR = ActiveWorkbook
Set WSD = WBR.Worksheets("Data")
Set WSR = WBR.Worksheets("Request Review")
Set CurRec = WSR.Range("CurrRec")
Set Cmt = WSR.Range("Comment")
Set LastRec = WSR.Range("TotalRec")
 
iRow = CurRec.Value

Application.ScreenUpdating = False
 
If WSR.Range("G6") <> "Submitted" Then
    MsgBox "This request has already been reviewed & responded to."
    Exit Sub
End If
 
Set WBD = Workbooks.Open(Filename:="G:\U\Dashboard\Time Off\Master\TOATData.xlsx")

Set WSData = WBD.Worksheets("Data") 'Data worksheet

'On Error GoTo 0
'Select the Data worksheet
WSData.Select
 
WSData.AutoFilter.ShowAllData
 
WSData.Select

FinalRow = WSData.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To FinalRow
    If Cells(i, 1) = iRow Then
        Cells(i, 14) = "Approved"
        Cells(i, 15) = Cmt.Value
        Cells(i, 16) = Now()
    End If
Next i
 
WBD.Save
 
WSData.Cells.Copy
 
WBR.Activate

WSD.Select

Cells(1, 1).Select
 
Selection.PasteSpecial xlPasteValuesAndNumberFormats 
Application.CutCopyMode = False
 
WBD.Close True
 
WSD.Activate
 
WSD.Columns.AutoFit
 
WSD.Columns("M:M").ColumnWidth = 50
 
WSD.Range("A1").AutoFilter
 
WSD.Range("C2").Select

ActiveWindow.FreezePanes = True
 
WSR.Select

Cmt.Value = ""
 
Call StatusEmail
 
If CurRec < LastRec Then
    CurRec = CurRec + 1
End If
 
Application.ScreenUpdating = True
End Sub

Thank you.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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