L
LS
Hi - I have another question which I hope someone can assist with.
I have a macro which will search a separate worksheet for values in
one column, then use autofilter to select all identical values in
the same column in the current worksheet and delete them. The
worksheets are both sorted so that Columns A & B are in
alphabetical order, to speed processing.
It works - but I am trying to speed it up. With 20,000 rows on the
worksheet, on my PC it is taking over 30 seconds to delete the rows.
At work it will take at least twice as long. Not disastrous, but if
I can speed it up, so much the better. The delay seems to be in the
deletion of the rows - the tests I have made show the 'For each '
loop will test the values in less than a second, its only when
it has to manipulate the sheet that it slows down.
Here's an extract of my code:
(NB Range 'ImportDataFilter' is a named range on the worksheet including
all data and a header row)
LastRow = Workbooks("New").Sheets("Sheet1").Cells
(Cells.Rows.Count, "A").End(xlUp).Row
Set MyRange = Workbooks("New").Worksheets("Sheet1").Range
("B8:B" & LastRow)
RegionToAdd = Workbooks("New").Sheets("Sheet1").Range("A8").Value
LocationToAdd = Workbooks("New").Sheets("Sheet1").Range("B8").Text
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("Sheet1").Activate
ActiveSheet.Unprotect PASSWORD:=PASSWORD
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
MyCount = 8 'data starts at row 8
For Each C In MyRange
D = C.Value
If LocationToAdd <> D Then ' ie this is a new location so
D is 1 more than last correct cell
ThisWorkbook.Worksheets("Sheet1").Activate
ThisWorkbook.Worksheets("Sheet1").Range("ImportDataFilter").Select
Selection.AutoFilter Field:=1, Criteria1:=RegionToAdd
Selection.AutoFilter Field:=2, Criteria1:=LocationToAdd
ThisWorkbook.Worksheets("Sheet1").Range("DATA").SpecialCells
(xlCellTypeVisible).Delete Shift:=xlUp
LocationToAdd = Workbooks("New").Worksheets("Sheet1").Range
("B" & MyCount).Value
RegionToAdd = Workbooks("New").Worksheets("Sheet1").Range
("A" & MyCount).Value
End If
MyCount = MyCount + 1
Next C
code continues ...
I have tried deleting the entire row instead of the cells, but get
an error message.
Any suggestions to speed this up gratefully received!
LS
I have a macro which will search a separate worksheet for values in
one column, then use autofilter to select all identical values in
the same column in the current worksheet and delete them. The
worksheets are both sorted so that Columns A & B are in
alphabetical order, to speed processing.
It works - but I am trying to speed it up. With 20,000 rows on the
worksheet, on my PC it is taking over 30 seconds to delete the rows.
At work it will take at least twice as long. Not disastrous, but if
I can speed it up, so much the better. The delay seems to be in the
deletion of the rows - the tests I have made show the 'For each '
loop will test the values in less than a second, its only when
it has to manipulate the sheet that it slows down.
Here's an extract of my code:
(NB Range 'ImportDataFilter' is a named range on the worksheet including
all data and a header row)
LastRow = Workbooks("New").Sheets("Sheet1").Cells
(Cells.Rows.Count, "A").End(xlUp).Row
Set MyRange = Workbooks("New").Worksheets("Sheet1").Range
("B8:B" & LastRow)
RegionToAdd = Workbooks("New").Sheets("Sheet1").Range("A8").Value
LocationToAdd = Workbooks("New").Sheets("Sheet1").Range("B8").Text
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("Sheet1").Activate
ActiveSheet.Unprotect PASSWORD:=PASSWORD
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
MyCount = 8 'data starts at row 8
For Each C In MyRange
D = C.Value
If LocationToAdd <> D Then ' ie this is a new location so
D is 1 more than last correct cell
ThisWorkbook.Worksheets("Sheet1").Activate
ThisWorkbook.Worksheets("Sheet1").Range("ImportDataFilter").Select
Selection.AutoFilter Field:=1, Criteria1:=RegionToAdd
Selection.AutoFilter Field:=2, Criteria1:=LocationToAdd
ThisWorkbook.Worksheets("Sheet1").Range("DATA").SpecialCells
(xlCellTypeVisible).Delete Shift:=xlUp
LocationToAdd = Workbooks("New").Worksheets("Sheet1").Range
("B" & MyCount).Value
RegionToAdd = Workbooks("New").Worksheets("Sheet1").Range
("A" & MyCount).Value
End If
MyCount = MyCount + 1
Next C
code continues ...
I have tried deleting the entire row instead of the cells, but get
an error message.
Any suggestions to speed this up gratefully received!
LS