VBA code to delete rows based on certain criteria (match case)

deletedalien

Well-known Member
Joined
Dec 8, 2008
Messages
505
Office Version
  1. 2013
Platform
  1. Windows
Hi there,

so i have a couple of questions but they the end result is basically the same...

i need to delete rows...

firstly i would need a macro to delete all rows that contain a lowercase Z
(usually should be found in column "A")

Next i would need to delete all rows that contain an empty cell in a certain column...
(also varies)

lastly i would need to delete all rows in column "E" that contain Zeros but ONLY if the value in column "D" has a value greater than zero..



now i don't know if the first 2 can be combined using an inputbox.

but im pretty sure the last one has to be a separate macro which is totally ok.

i hope i have provided enough information, otherwise please feel free to let me know.

Thanks in advance for any help.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
Sub DeleteMyRows()
Dim r As Long, iRows As Long
Dim vWord
Const kColD = 3
Const kColE = 4
Const kColF = 5


iRows = ActiveSheet.UsedRange.Rows.Count
Range("A" & iRows).Select


Sheets("sheet1").Select


  'we must delete in reverse row order..duh.
For r = iRows To 2 Step -1
   Cells(r, 1).Select
   vWord = Trim(ActiveCell.Value)
   
      'Delete if the whole cell is 'z' OR the cell has a word and the word has a 'z'???
   If vWord = LCase("z") Then Del1Row r
   
         'delete row if cell F is blank
   'If ActiveCell.Offset(0, kColF) = "" Then Del1Row r
   
       'delete row if cell E =zero AND col D>0
   If ActiveCell.Offset(0, kColE).Value = 0 And ActiveCell.Offset(0, kColD).Value > 0 Then Del1Row r
Next
End Sub


Private Sub Del1Row(r)
Rows(r & ":" & r).Delete
End Sub
 
Upvote 0
Solution
Code:
Sub DeleteMyRows()
Dim r As Long, iRows As Long
Dim vWord
Const kColD = 3
Const kColE = 4
Const kColF = 5


iRows = ActiveSheet.UsedRange.Rows.Count
Range("A" & iRows).Select


Sheets("sheet1").Select


  'we must delete in reverse row order..duh.
For r = iRows To 2 Step -1
   Cells(r, 1).Select
   vWord = Trim(ActiveCell.Value)
   
      'Delete if the whole cell is 'z' OR the cell has a word and the word has a 'z'???
   If vWord = LCase("z") Then Del1Row r
   
         'delete row if cell F is blank
   'If ActiveCell.Offset(0, kColF) = "" Then Del1Row r
   
       'delete row if cell E =zero AND col D>0
   If ActiveCell.Offset(0, kColE).Value = 0 And ActiveCell.Offset(0, kColD).Value > 0 Then Del1Row r
Next
End Sub


Private Sub Del1Row(r)
Rows(r & ":" & r).Delete
End Sub

i know im REALLY dumb but...

i get an error....


Sub or function not defined :(
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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