Help speeding up VBA Script

rholdren

Board Regular
Joined
Aug 25, 2016
Messages
140
Office Version
  1. 365
  2. 2019
Good morning,

I have Office 365 Excel and seem to be having a little issue with the scrip below. It runs extremely slow and it is only pulling the first record over and stopping. The script should be reading down column AX and and wherever it finds 3% should copy the entire row and move it to another worksheet.

Any help on this would be greatly appreciated.

Thanks (y)

Sub Create_List()

Application.ScreenUpdating = False

Sheets("OptumRxExport").Select


Last = Cells(Rows.Count, "AX").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "AX").Value) = "3%" Then

Cells(i, "AX").Select


Cells(i, "AX").EntireRow.Copy


Sheets("Discounted_List").Select
Range("a2:I50000").Select


Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.PasteSpecial Paste:=xlPasteValues
ActiveCell.PasteSpecial Paste:=xlPasteFormats



End If

Sheets("OptumRxExport").Select


Next i

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
If you still want to loop backwards then maybe:
VBA Code:
Sub Create_List()
    Dim wsORxEx As Worksheet, wsDL As Worksheet, Last As Long
   
    Set wsORxEx = Sheets("OptumRxExport")
    Set wsDL = Sheets("Discounted_List")
    Last = wsORxEx.Cells(Rows.Count, "AX").End(xlUp).Row
   
    For i = Last To 1 Step -1
        If wsORxEx.Cells(i, "AX").Value = "3%" Then
        wsORxEx.Rows(i).Copy
        wsDL.Range("A" & wsDL.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValuesAndNumberFormats
    Next i
End Sub

Else you could loop forwards with a range loop:
VBA Code:
Sub Create_List2()
    Dim wsORxEx As Worksheet, wsDL As Worksheet, rCell As Range
  
    Set wsORxEx = Sheets("OptumRxExport")
    Set wsDL = Sheets("Discounted_List")
  
    For Each rCell In wsORxEx.Range("AX1:AX" & wsORxEx.Cells(Rows.Count, "AX").End(xlUp).Row)
        If rCell = "3%" Then
        rCell.EntireRow.Copy
        wsDL.Range("A" & wsDL.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValuesAndNumberFormats
    Next i
End Sub
 
Upvote 0
Another option that may be faster is to use autofilter and then copy the visible cells and paste them elsewhere.
 
Upvote 0
I think perhaps a filter solution would be much more efficient (filter all the rows where the value in that cell = 3%).
Advanced filters let you copy to a different location.
 
Upvote 0
Here is an autofilter version:
VBA Code:
Sub Create_List3()
    Dim wsORxEx As Worksheet, wsDL As Worksheet, rng As Range, rng2 As Range
    
    Set wsORxEx = Sheets("OptumRxExport")
    Set wsDL = Sheets("Discounted_List")
    Set rng = wsORxEx.UsedRange
    Set rng2 = rng.Offset(1).Resize(rng.Rows.Count - 1, rng.Columns.Count)
    
    If Not ActiveSheet.AutoFilterMode Then
        wsORxEx.Range("A1").AutoFilter
    End If
    rng.AutoFilter 50, "3%"
    rng2.SpecialCells(xlCellTypeVisible).Copy
    wsDL.Range("A" & wsDL.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False
End Sub
 
Upvote 0
In the first 2 I am getting Next without For error and in the third one I'm getting a AutoFilter method of Range Class Failed error
 
Upvote 0
My error there on the first two, an issue with writing untested code - i missed an end if:
VBA Code:
Sub Create_List()
    Dim wsORxEx As Worksheet, wsDL As Worksheet, Last As Long
   
    Set wsORxEx = Sheets("OptumRxExport")
    Set wsDL = Sheets("Discounted_List")
    Last = wsORxEx.Cells(Rows.Count, "AX").End(xlUp).Row
   
    For i = Last To 1 Step -1
        If wsORxEx.Cells(i, "AX").Value = "3%" Then
            wsORxEx.Rows(i).Copy
            wsDL.Range("A" & wsDL.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValuesAndNumberFormats
        End If
    Next i
End Sub

VBA Code:
Sub Create_List2()
    Dim wsORxEx As Worksheet, wsDL As Worksheet, rCell As Range
  
    Set wsORxEx = Sheets("OptumRxExport")
    Set wsDL = Sheets("Discounted_List")
  
    For Each rCell In wsORxEx.Range("AX1:AX" & wsORxEx.Cells(Rows.Count, "AX").End(xlUp).Row)
        If rCell = "3%" Then
            rCell.EntireRow.Copy
            wsDL.Range("A" & wsDL.Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValuesAndNumberFormats
        End If
    Next i
End Sub
 
Upvote 0
For the filter version change the below:
VBA Code:
rng.AutoFilter 50, "3%"
To
VBA Code:
rng.AutoFilter 37, "3%"

Not sure why but i thought column AK was 50...
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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