Sort an Delete rows using a Macro

monkeyharris

Active Member
Joined
Jan 20, 2008
Messages
358
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a Macro which sorts and deletes rows based on one criteria but now i need to sort Column E then delete all Rows which DON'T say the work "CPI". Is this possible. I can do it so it will delete anything which =CPI but the <>CPI method won't work. PLEASE HELP. This is the Macro:

Sub M1()


Dim LR As Long, LC As Long


Application.ScreenUpdating = False


With ActiveSheet
LR = .Range("L" & .Rows.Count).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
If .AutoFilterMode Then .AutoFilterMode = False
With .Range("A1").Resize(LR, LC)
.Sort key1:=ActiveSheet.Range("E1"), order1:=xlAscending, Header:=xlYes
.AutoFilter
.AutoFilter field:=ActiveSheet.Range("E1").Column, Criteria1:=CPI
.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
.AutoFilterMode = False
End With


Application.ScreenUpdating = True


End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this:
Code:
Sub DeleteRowsThatDontEqualCPI()
        
    Dim MyRange As Range
    Dim MyTotal As Long
    
    Set MyRange = Range("E1:E1000")
    MyTotal = MyRange.Cells.Count
    
    For MyCount = MyTotal To 1 Step -1
        If MyRange.Cells(MyCount) <> "CPI" Then
             MyRange.Cells(MyCount).EntireRow.Delete
        End If
    Next
    
End Sub

You have to delete rows from the bottom to the top of the worksheet, otherwise it doesn't work. Just change the reference in MyRange to suit your needs.

Hope this helps

Pete
 
Upvote 0
Without looping and changes in blue, try:
Rich (BB code):
Sub M1()

Dim rng As Range
Dim LR As Long, LC As Long

Application.ScreenUpdating = False

With ActiveSheet
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If .AutoFilterMode Then .AutoFilterMode = False
    With .Range("A1").Resize(LR, LC)
        .Sort key1:=ActiveSheet.Range("E1"), order1:=xlAscending, Header:=xlYes
        .AutoFilter
        .AutoFilter field:=ActiveSheet.Range("E1").Column, Criteria1:="<>CPI"
        .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
   End With
   If .FilterMode Then .ShowAllData
   .AutoFilterMode = False
End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi Pete & Dan,

They both work to a degree but there is one problem with both. Dan, yours does everything but leaves the cell border where the deleted cells were. Pete, yours deleted all the lines OK but also deleted the first line which also contains the headings.
 
Upvote 0
MonkeyHarris,

Did you change
Code:
Set MyRange = Range("E1:E1000")
to:
Code:
Set MyRange = Range("E2:E1000")
[/COLOR]


? :)

Pete
 
Last edited:
Upvote 0
Change in blue, try:
Rich (BB code):
Sub M1()

Dim LR As Long, LC As Long

Application.ScreenUpdating = False

With ActiveSheet
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
    If .AutoFilterMode Then .AutoFilterMode = False
    With .Range("A1").Resize(LR, LC)
        .Sort key1:=ActiveSheet.Range("E1"), order1:=xlAscending, Header:=xlYes
        .AutoFilter
        .AutoFilter field:=ActiveSheet.Range("E1").Column, Criteria1:="<>CPI"
        With .Offset(1).SpecialCells(xlCellTypeVisible)
            .Borders.LineStyle = xlNone
            .EntireRow.Delete
        End With
   End With
   If .FilterMode Then .ShowAllData
   .AutoFilterMode = False
End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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