简体   繁体   中英

Delete row in Excel if a cell does not contain a specific value

I have a worksheet, i need deleted all rows that not contain the phrase "@" on column G until column K.

This is sample data i have

http://i.stack.imgur.com/MfqUM.png

if cell on Column G until Column K not containing "@", Delete Rows

please help me..

The following macro scans from row 2 and removes rows where columns 2-5 do not contain an @ :

Sub Macro1()
CurRow = 2
While CurRow < Application.WorksheetFunction.CountA(Range("A:A")) + 1
  If InStr(1, _
      Sheet1.Cells(CurRow, 2) _
      & Sheet1.Cells(CurRow, 3) _
      & Sheet1.Cells(CurRow, 4) _
      & Sheet1.Cells(CurRow, 5), _
      "@", vbTextCompare) Then
  Else
    Sheet1.Rows(CurRow & ":" & CurRow).Delete Shift:=xlUp
    CurRow = CurRow - 1
  End If
  CurRow = CurRow + 1
Wend
End Sub

Using Instr , we concatenate the columns of interest (2-5 in my example code above) to create a single string of text. Then we search for @ within that concatenated string and remove accordingly.

It changes

在此处输入图片说明

into

在此处输入图片说明


Alternatively, you can create an additional column containing the following formula:

在此处输入图片说明

FIND is used to identify whether an @ exists within one of the preceding cells, print No if not and Yes if it does. Now you can filter these elements ( No ), select the range (making sure only the visible cells are selected), delete the rows and remove the filter.

You will have to use several loop commands. Begin by looping through the records that you want to process. Here is an outline of pseudo-code.

  • loop through the columns that you want to process, G through K
  • read the value of the cell as a string
  • initialize variable atdetector = "dunno_yet"
  • loop through each character of the string
  • if any character = "@"; then atdetector = "found"

After looping through each column, if atdetector = "dunno_yet" , then "@" was never found. Delete the row. Repeat until there are no more rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM