Ankitsahgal
New Member
- Joined
- Dec 19, 2015
- Messages
- 11
Hello,
I am newbie to writing macros and still trying to get my head around dimensions. Whilst I do not understand much about them, I know that it important to define it correctly otherwise it causes the macro to run very slow.
I have written my first code and it all goes fine until ''Filter 4, from which point the code runs really slow. The filter 4 is just copying the formula from the active cell A2 and autofill it all the way down until the last data row in A.
Any advice on how to clean the code and make it run faster would be greatly appreciated.
Thanks!
Ankit
Sub AgedDebtorFinal()
Dim ws As Worksheet
Dim Rng As Range
'Copy and Rename sheet
Sheets("Aged Debtors Inv Date").Select
Sheets("Aged Debtors Inv Date").Copy before:=Sheets(3)
Sheets("Aged Debtors Inv Date (2)").Select
Sheets("Aged Debtors Inv Date (2)").Name = "Summary"
'Paste Values
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Unhide and delete rows
Rows("1:68").Select
Selection.Delete Shift:=xlUp
Range("C2:C5").Select
Selection.EntireRow.Delete
Columns("A:A").Select
Selection.EntireColumn.Hidden = False
'Filter and Sort
''Filter 1
Range("B1") = "FILTER"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""INSERTED GROUP"",1,""IGNORE"")"
Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.EntireRow.Delete
''Filter 2
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""INSERTED CONTINUATION"",1,""IGNORE"")"
Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.EntireRow.Delete
''Filter 3
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""INSERTED FOOTER"",TRIM(R[-1]C[1]),1)"
Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
''Filter 4
Range("A2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[1]=1,1,""IGNORE"")"
Range("A2").Select
Range("A2").AutoFill Destination:=Range("A2:A" & Cells(Rows.Count, "B").End(xlDown).Row)
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.EntireRow.Delete
Application.CutCopyMode = False
''Delete all the rows and columns
Columns("A").Select
Selection.EntireColumn.Delete
Columns("B:G").Select
Selection.EntireColumn.Delete
Columns("H:J").Select
Selection.EntireColumn.Delete
''Rename
Range("A1") = "Customer"
Range("G1") = "Total"
''Format
Cells.Select
With Selection.Font
.Name = "Calibri"
.Size = 12
.Bold = False
.TintAndShade = 0
End With
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Borders
.LineStyle = 0
End With
End Sub
I am newbie to writing macros and still trying to get my head around dimensions. Whilst I do not understand much about them, I know that it important to define it correctly otherwise it causes the macro to run very slow.
I have written my first code and it all goes fine until ''Filter 4, from which point the code runs really slow. The filter 4 is just copying the formula from the active cell A2 and autofill it all the way down until the last data row in A.
Any advice on how to clean the code and make it run faster would be greatly appreciated.
Thanks!
Ankit
Sub AgedDebtorFinal()
Dim ws As Worksheet
Dim Rng As Range
'Copy and Rename sheet
Sheets("Aged Debtors Inv Date").Select
Sheets("Aged Debtors Inv Date").Copy before:=Sheets(3)
Sheets("Aged Debtors Inv Date (2)").Select
Sheets("Aged Debtors Inv Date (2)").Name = "Summary"
'Paste Values
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Unhide and delete rows
Rows("1:68").Select
Selection.Delete Shift:=xlUp
Range("C2:C5").Select
Selection.EntireRow.Delete
Columns("A:A").Select
Selection.EntireColumn.Hidden = False
'Filter and Sort
''Filter 1
Range("B1") = "FILTER"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""INSERTED GROUP"",1,""IGNORE"")"
Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.EntireRow.Delete
''Filter 2
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""INSERTED CONTINUATION"",1,""IGNORE"")"
Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.EntireRow.Delete
''Filter 3
Range("B2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""INSERTED FOOTER"",TRIM(R[-1]C[1]),1)"
Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
''Filter 4
Range("A2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[1]=1,1,""IGNORE"")"
Range("A2").Select
Range("A2").AutoFill Destination:=Range("A2:A" & Cells(Rows.Count, "B").End(xlDown).Row)
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.EntireRow.Delete
Application.CutCopyMode = False
''Delete all the rows and columns
Columns("A").Select
Selection.EntireColumn.Delete
Columns("B:G").Select
Selection.EntireColumn.Delete
Columns("H:J").Select
Selection.EntireColumn.Delete
''Rename
Range("A1") = "Customer"
Range("G1") = "Total"
''Format
Cells.Select
With Selection.Font
.Name = "Calibri"
.Size = 12
.Bold = False
.TintAndShade = 0
End With
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Borders
.LineStyle = 0
End With
End Sub