PDA

View Full Version : How to delete entire row if specific column has ANY data in it?



sean.golds
08-01-2016, 04:15 AM
Hi everyone,

I could use a little help here if possible. I know how to delete a row if a column has SPECIFIC text in it, however, I want the row to be deleted if the column has anything in it at all... so the code i have for deleting something with specific text is as follows:


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
'We check the values in the H column in this example
With .Cells(Lrow, "H")
If Not IsError(.Value) Then
If .Value = "ron" Then .EntireRow.Delete
'This will delete each row with the Value "ron"
'in Column A, case sensitive.
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub

Obviously i want to replace "ron" with whatever is needed for any data in the cell whatsoever to result in the row being deleted...

please help!

Thanks

vcoolio
08-01-2016, 04:35 AM
Hello Sean,

If your code is working OK, you should only have to change the following line:-


If .Value = "ron" Then .EntireRow.Delete

to


If .Value <> "" Then .EntireRow.Delete

I hope that this helps.

Cheerio,
vcoolio.

sean.golds
08-01-2016, 04:43 AM
That worked whats frustrating is i reckoned to have tried that earlier and it didn't which obviously means I had made a mistake somewhere...

Thank you very much!!

sean.golds
08-01-2016, 04:59 AM
Sorry but i would like to run the above macro in a separate excel file literally by clicking a button. So it would open the excel file in question, run that macro, save the updated file and close it. Any help?

vcoolio
08-02-2016, 12:30 AM
Hello Sean,

I'm somewhat tied up for quite a while but, in the meantime, have a look at the following link:-

http://www.mrexcel.com/forum/excel-questions/734107-using-macro-within-file-open-run-another-macro-enabled-workbook.html

The query, I believe, is similar to yours so try and adapt the code (a couple of variations are supplied in the link) to your work book and let us know how you get on.

Cheerio,
vcoolio.