My data extraction macro runs VERY slowly and it bums me out.

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
I'm using this code to scan a named range and extract data from rows that contain a "/" in a specific column. The macro works fine but takes roughly 20 seconds to scan about 730 rows. Here's the code:

VBA Code:
Sub RunPlanRequests()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Sheets("FloorPlanRequests").Visible = True
    Sheets("FloorPlanRequests").Select
    
    Dim MR As Excel.Range
    Dim rngCell As Excel.Range
    Dim rngCount As Long
    Dim ws As Worksheet
    
    Set ws = Sheets("Calendar")
    Set MR = ws.Range("CalendarFloorsOrderNumberColumn")
    
    rngCount = 1
    
    For Each rngCell In MR
        If rngCell.Value Like "*/*" Then
            With Sheets("FloorPlanRequests").Range("A" & rngCount)
               .Value = rngCell.Value
               .Offset(, 1).Resize(, 3).Value = Array(ws.Range("F" & rngCell.Row).Value, ws.Range("AS" & rngCell.Row).Value, ws.Range("B" & rngCell.Row).Value)
            End With
            rngCount = rngCount + 1
        End If
    Next rngCell
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What range does "CalendarFloorsOrderNumberColumn" refer to?
 
Upvote 0
Try:
VBA Code:
Sub RunPlanRequests()
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    Dim MR As Variant, lRow As Long, wsCal As Worksheet, i As Long, desWS As Worksheet, rCnt As Long: rCnt = 1
    Sheets("FloorPlanRequests").Visible = True
    Set desWS = Sheets("FloorPlanRequests")
    Set wsCal = Sheets("Calendar")
    With wsCal
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        MR = .Range("B5:B" & lRow).Resize(, 44).Value
    End With
    For i = LBound(MR) To UBound(MR)
        If MR(i, 17) Like "*/*" Then
            With desWS
                .Range("A" & rCnt).Resize(, 4).Value = Array(MR(i, 17), MR(i, 5), MR(i, 44), MR(i, 1))
                rCnt = rCnt + 1
            End With
        End If
    Next i
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub RunPlanRequests()
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    Dim MR As Variant, lRow As Long, wsCal As Worksheet, i As Long, desWS As Worksheet, rCnt As Long: rCnt = 1
    Sheets("FloorPlanRequests").Visible = True
    Set desWS = Sheets("FloorPlanRequests")
    Set wsCal = Sheets("Calendar")
    With wsCal
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        MR = .Range("B5:B" & lRow).Resize(, 44).Value
    End With
    For i = LBound(MR) To UBound(MR)
        If MR(i, 17) Like "*/*" Then
            With desWS
                .Range("A" & rCnt).Resize(, 4).Value = Array(MR(i, 17), MR(i, 5), MR(i, 44), MR(i, 1))
                rCnt = rCnt + 1
            End With
        End If
    Next i
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub

Much better. Now takes about 5 seconds. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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