Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete row based on cell value

Tags:

excel

vba

I have a worksheet, I need to delete rows based on cell value ..

Cells to check are in Column A ..

If cell contains "-" .. Delete Row

I can't find a way to do this .. I open a workbook, copy all contents to another workbook, then delete entire rows and columns, but there are specific rows that has to be removed based on cell value.

Need Help Here.

UPDATE

Sample of Data I have

Sample

like image 246
sikas Avatar asked Jan 30 '13 20:01

sikas


People also ask

How do I delete an entire row in Excel based on a cell value?

Once you have the blank cells selected, right-click on any of the cells and click on Delete. In the Delete dialog box, select the 'Entire row' option and click OK. This will delete all rows that have blank cells in it.

How do I delete rows if a cell contains specific text in Excel?

To delete rows that contain these cells, right-click anywhere in the data range and from the drop-down menu, choose Delete.


4 Answers

The easiest way to do this would be to use a filter.

You can either filter for any cells in column A that don't have a "-" and copy / paste, or (my more preferred method) filter for all cells that do have a "-" and then select all and delete - Once you remove the filter, you're left with what you need.

Hope this helps.

like image 192
John Bustos Avatar answered Oct 18 '22 21:10

John Bustos


The screenshot was very helpful - the following code will do the job (assuming data is located in column A starting A1):

Sub RemoveRows()

Dim i As Long

i = 1

Do While i <= ThisWorkbook.ActiveSheet.Range("A1").CurrentRegion.Rows.Count

    If InStr(1, ThisWorkbook.ActiveSheet.Cells(i, 1).Text, "-", vbTextCompare) > 0 Then
        ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete
    Else
        i = i + 1
    End If

Loop

End Sub

Sample file is shared: https://www.dropbox.com/s/2vhq6vw7ov7ssya/RemoweDashRows.xlsm

like image 40
Peter L. Avatar answered Oct 18 '22 22:10

Peter L.


You could copy down a formula like the following in a new column...

=IF(ISNUMBER(FIND("-",A1)),1,0)

... then sort on that column, highlight all the rows where the value is 1 and delete them.

like image 9
weir Avatar answered Oct 18 '22 21:10

weir


if you want to delete rows based on some specific cell value. let suppose we have a file containing 10000 rows, and a fields having value of NULL. and based on that null value want to delete all those rows and records.

here are some simple tip. First open up Find Replace dialog, and on Replace tab, make all those cell containing NULL values with Blank. then press F5 and select the Blank option, now right click on the active sheet, and select delete, then option for Entire row.

it will delete all those rows based on cell value of containing word NULL.

like image 5
zaheer Avatar answered Oct 18 '22 21:10

zaheer