Deleting rows

  • Thread starter Thread starter JKWParrott
  • Start date Start date
J

JKWParrott

I'm trying to figure out if I can set up a macro to delete rows if they do
not contain anything in specific columns.

For example, I have product codes in column A and totals for on-hand and
ordered numbers in columns B and C. If some rows do not have on-hand or
ordered numbers I want to delete those rows. Any ideas?
 
Start here
http://www.rondebruin.nl/delete.htm

For example in the first macro use
Change the range in this line that you want to test
If Application.CountA(.Cells(Lrow, 1).Range("A1,M1,X1")) = 0 Then .Rows(Lrow).Delete


Sub Loop_Example()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet

'We select the sheet so we can change the window view
.Select

'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView

'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False

'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1

If Application.CountA(.Cells(Lrow, 1).Range("A1,M1,X1")) = 0 Then .Rows(Lrow).Delete
'This will delete the row if the cells in A, M and X in the row are empty

Next Lrow

End With

ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With

End Sub
 
If some rows do not have on-hand or
ordered numbers I want to delete those rows.

I note you said "or" and not "and" in the above, so I'm guessing that within
a given row, if either the cell in Column B **or** the cell in Column C (but
not necessarily both) are empty, then you want the entire row deleted. I'm
guessing the should work for you...

Sub DeleteUnneededRows()
On Error Resume Next
Columns("B:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Thanks Ron, I was not aware of the xlSpecialCellsType limitation. Could
save me some grief in the future.
 
Back
Top