Nameless_87
New Member
- Joined
- Dec 19, 2012
- Messages
- 42
Hello All,
This will probably be an easy request for you.
I am currently running a script that filters a rather large report I run to a manageable size. It runs from around 40000-50000 rows down to 2000-5000.
Is there any way of speeding the script up? It takes about 20 mins to run currently.</SPAN></SPAN>
Sub SortReport()
'
' SortReport Macro
Workbooks.Open Filename:="*Selected Drive*\Exported Reports.xls"
Application.ScreenUpdating = False 'These two bits of code speed up the macro
'Application.Calculation = xlManual
Cells.Select
Selection.Copy
Windows("Candidate Checker V3.xlsm").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("B:H").Select
Columns("B:H").EntireColumn.AutoFit 'Formatas data in range
'We need to consolicate the candidate columns here
Sheets("Sheet2").Select
'Insert new column for Concatenating First Name and Last Name:
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Insert the concatenate formula:
Range("C2:C50000").Select
'Format range as general to allow formula to be entered:
Selection.NumberFormat = "General"
ActiveCell.Formula = Formula 'R1C1 = ""
Range("C4").Select
'ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],"" "",RC[-1])"
ActiveCell.Formula = "=CONCATENATE(RC[-1],"" "",RC[-2])"
Range("C4").Select
'filldown:
Selection.AutoFill Destination:=Range("C4:C50000") 'unable to find working code to filldown dynamic range:
'Range("C2").AutoFill Destination:=Range("C2:C" & Lastrow)? This seems to screw up the macro
Range("C4:C50000").Select
'Enter column heading, and autofit:
Range("C3").Select
ActiveCell.FormulaR1C1 = "Full Name"
Columns("C:C").EntireColumn.AutoFit
'Copy concatenated names, then overwrite with Paste Values:
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Exported Reports.xls").Activate
Application.DisplayAlerts = False
ActiveWindow.Close SaveChanges = False
Columns("C:C").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A2").Select
Selection.AutoFilter
'Filter Names
Dim v As Variant
'There are a roughly 350 FullNames
v = Application.Transpose(Range("FullNames"))
Range("A2").Select
Selection.AutoFilter Field:=1, Criteria1:=v, Operator:=xlFilterValues
Dim Firstrow As Long ' This section removes courses and any sessions not wanted
Dim LastRow As Long ' by deleting the black rows
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With Sheets("sheet2")
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = LastRow To Firstrow Step -1
'We check the values in the B column in this example
With .Cells(Lrow, "F")
If Not IsError(Application.Match(.Value, _
Sheets("Candidates").Range("Cancelled"), 0)) Then .EntireRow.Delete
End With
With .Cells(Lrow, "D")
' The "NotRequired" list is roughly 1650 lines
If Not IsError(Application.Match(.Value, _
Sheets("Candidates").Range("NotRequired"), 0)) Then .EntireRow.Delete
End With
With .Cells(Lrow, "D")
If IsEmpty(.Value) Then .EntireRow.Delete
'This will delete the row if the cell is empty
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Select
Range("B5").Select
ActiveSheet.Paste
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").ColumnWidth = 8.5
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Sheets("Sheet2").Select
Selection.AutoFilter
Sheets("Sheet1").Select
Application.Calculation = xlAutomatic 'This closes the speed of editing
Application.ScreenUpdating = True
End Sub
This will probably be an easy request for you.
I am currently running a script that filters a rather large report I run to a manageable size. It runs from around 40000-50000 rows down to 2000-5000.
Is there any way of speeding the script up? It takes about 20 mins to run currently.</SPAN></SPAN>
Sub SortReport()
'
' SortReport Macro
Workbooks.Open Filename:="*Selected Drive*\Exported Reports.xls"
Application.ScreenUpdating = False 'These two bits of code speed up the macro
'Application.Calculation = xlManual
Cells.Select
Selection.Copy
Windows("Candidate Checker V3.xlsm").Activate
Sheets("Sheet2").Select
Cells.Select
ActiveSheet.Paste
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("B:H").Select
Columns("B:H").EntireColumn.AutoFit 'Formatas data in range
'We need to consolicate the candidate columns here
Sheets("Sheet2").Select
'Insert new column for Concatenating First Name and Last Name:
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Insert the concatenate formula:
Range("C2:C50000").Select
'Format range as general to allow formula to be entered:
Selection.NumberFormat = "General"
ActiveCell.Formula = Formula 'R1C1 = ""
Range("C4").Select
'ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],"" "",RC[-1])"
ActiveCell.Formula = "=CONCATENATE(RC[-1],"" "",RC[-2])"
Range("C4").Select
'filldown:
Selection.AutoFill Destination:=Range("C4:C50000") 'unable to find working code to filldown dynamic range:
'Range("C2").AutoFill Destination:=Range("C2:C" & Lastrow)? This seems to screw up the macro
Range("C4:C50000").Select
'Enter column heading, and autofit:
Range("C3").Select
ActiveCell.FormulaR1C1 = "Full Name"
Columns("C:C").EntireColumn.AutoFit
'Copy concatenated names, then overwrite with Paste Values:
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Exported Reports.xls").Activate
Application.DisplayAlerts = False
ActiveWindow.Close SaveChanges = False
Columns("C:C").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A2").Select
Selection.AutoFilter
'Filter Names
Dim v As Variant
'There are a roughly 350 FullNames
v = Application.Transpose(Range("FullNames"))
Range("A2").Select
Selection.AutoFilter Field:=1, Criteria1:=v, Operator:=xlFilterValues
Dim Firstrow As Long ' This section removes courses and any sessions not wanted
Dim LastRow As Long ' by deleting the black rows
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With Sheets("sheet2")
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = LastRow To Firstrow Step -1
'We check the values in the B column in this example
With .Cells(Lrow, "F")
If Not IsError(Application.Match(.Value, _
Sheets("Candidates").Range("Cancelled"), 0)) Then .EntireRow.Delete
End With
With .Cells(Lrow, "D")
' The "NotRequired" list is roughly 1650 lines
If Not IsError(Application.Match(.Value, _
Sheets("Candidates").Range("NotRequired"), 0)) Then .EntireRow.Delete
End With
With .Cells(Lrow, "D")
If IsEmpty(.Value) Then .EntireRow.Delete
'This will delete the row if the cell is empty
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet1").Select
Range("B5").Select
ActiveSheet.Paste
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").ColumnWidth = 8.5
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Sheets("Sheet2").Select
Selection.AutoFilter
Sheets("Sheet1").Select
Application.Calculation = xlAutomatic 'This closes the speed of editing
Application.ScreenUpdating = True
End Sub