Macro To Look At Specific Data In Cells And Delete Entire Rows

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I need a macro for something I do every day and I cant record it as it has different amounts everyday. I need the macro to look at column 'B' and any cell where the second digit is an R I need the entire row deleted. I then need it sorted smallest to largest by column 'D' (row 1 has headers).

Column D will then have values starting with a minus number gradually rising to positive numbers, I then need anything with a zero or higher those rows totally deleted, keeping the rows with a minus value in column 'D'.

The format of the data in column 'B' is normally 2 letters then 6 numbers i.e MR123456, JD456789, XX123456 etc...

Is this possible?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Maybe

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Mid(Range("B" & i).Value, 2, 1) = "R" Then Rows(i).Delete
Next i
Range("A1").CurrentRegion.Sort Key1:=Range("D1"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0
Thanks VoG, almost there. It deletes the rows with the 'R' and then sorts column 'D' so the negatives are at the top. It doesn't however delete the rows that have a zero or positive number in 'D'. I know I can do that myself but it just needs a little tweak.
 
Upvote 0
Sorry, I missed that bit. try

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Mid(Range("B" & i).Value, 2, 1) = "R" Then Rows(i).Delete
Next i
Range("A1").CurrentRegion.Sort Key1:=Range("D1"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("D" & i).Value >= 0 Then Rows(i).Delete
Next i
End Sub
 
Upvote 0
Thanks, can it do just one more thing so I don't have to do anything! Once this has run can it then sort smallest to largest first by column 'G' then by column 'H' (inc headers in row 1) or can I record that bit and add it to the bottom?

Dont know if it makes a difference but the data may stretch as far as 'Z' so it will need to sort including all the columns inclusive.
 
Upvote 0
Thanks, can it do just one more thing so I don't have to do anything! Once this has run can it then sort smallest to largest first by column 'G' then by column 'H' (inc headers in row 1) or can I record that bit and add it to the bottom?

Dont know if it makes a difference but the data may stretch as far as 'Z' so it will need to sort including all the columns inclusive.

I did record it and added it on with the resulting code. Does it look ok?

I think I may have a problem though when I have different amounts of rows etc everyday.

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Mid(Range("B" & i).Value, 2, 1) = "R" Then Rows(i).Delete
Next i
Range("A1").CurrentRegion.Sort Key1:=Range("D1"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("D" & i).Value >= 0 Then Rows(i).Delete
Next i
Cells.Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("G2:G3114" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H2:H3114" _
        ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:N3114")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
I think I done it, I just changed the range from 3114 to 10000, which should cover all eventualities.
 
Upvote 0
I have played around with the code you gave me and it seems to do what I need it to do. I don't what all the different parts mean but I sort of used my common sense and tweaked the code you gave me. Could you check it for me and tell me where I have gone wrong if I have. I am quite chuffed actually all part of the learning curve.

Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Mid(Range("B" & i).Value, 2, 1) = "R" Then Rows(i).Delete
Next i
Range("A1").CurrentRegion.Sort Key1:=Range("D1"), Order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("D" & i).Value >= 0 Then Rows(i).Delete
Next i
LR = Range("G" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
If Range("G" & i).Value > 0 Then Rows(i).Delete
LR = Range("H" & Rows.Count).End(xlUp).Row
If Range("H" & i).Value > 0 Then Rows(i).Delete
LR = Range("C" & Rows.Count).End(xlUp).Row
If Range("C" & i).Value = "NPPACK" Then Rows(i).Delete
If Range("C" & i).Value = "EXPOSTER" Then Rows(i).Delete
If Range("C" & i).Value = "GYREGISTER" Then Rows(i).Delete
If Range("C" & i).Value = "INFOSHEET" Then Rows(i).Delete
    Next
End Sub
 
Upvote 0
ASAP Utilities might work for you I use it on a fairly regular basis and am amazed at the number of things it can do. It is always loaded in the menu and has a conditional delete feature.
 
Upvote 0
ASAP Utilities might work for you I use it on a fairly regular basis and am amazed at the number of things it can do. It is always loaded in the menu and has a conditional delete feature.

Sorry not come across those or know what they are.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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