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
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.
atdetector = "dunno_yet"
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.