1st Post: Delete rows from an array in excel IF row contains ANY string of text from a separate array?

RyanTG

New Member
Joined
Nov 29, 2015
Messages
42
Hello everyone!

Is it possible to delete rows from an array in excel IF it contains ANY string of text from a separate array?


Here is a sample of my data:

Excel Workbook
AB
1FirstEmail
2Ryanryantg@gmail.com
3Alalberk@gmail.com
4JimJimalco@gmail.com
Sheet6 (6)



My goal is for VBA to read this table and delete rows which contain any emails from another array.


A big thanks to all the labor and hard work from you excel philanthropists! You have been a gigantic help!

Thanks to your thorough work, this is the first post I have had to make :)
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
RyanTG,

Welcome to the MrExcel forum.

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


Thanks for the first screenshot.

Is it possible to delete rows from an array in excel IF it contains ANY string of text from a separate array?

Can we first have a screenshot of the other worksheet/array?

And, can we have another screenshot of worksheet Sheet6 (6), that will show the results?
 
Upvote 0
Thanks for your reply!

Here is the 2nd array of emails:

Excel Workbook
A
1Email
2ryantg@gmail.com
3Jimalco@gmail.com
Sheet1




1. Excel 2010; Windows 7 Pro

2. PC
 
Upvote 0
RyanTG,

Here is a macro for you to consider.

You can change the worksheet names in the macro.

Sample raw data worksheets:


Excel 2007
A
1Email
2ryantg@gmail.com
3Jimalco@gmail.com
4
Sheet1



Excel 2007
AB
1FirstEmail
2Ryanryantg@gmail.com
3Alalberk@gmail.com
4JimJimalco@gmail.com
5
Sheet6 (6)


And, after the macro:


Excel 2007
AB
1FirstEmail
2Alalberk@gmail.com
3
4
5
Sheet6 (6)


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub DeleteEmailRow()
' hiker95, 12/21/2015, ME910107
Dim w1 As Worksheet, wd As Worksheet
Dim c As Range, e As Range
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")       '<-- you can change the sheet name here
Set wd = Sheets("Sheet6 (6)")   '<-- you can change the sheet name here
With w1
  For Each c In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    Set e = wd.Columns(2).Find(c.Value, LookAt:=xlWhole)
    If Not e Is Nothing Then
      wd.Rows(e.Row).Delete
    End If
  Next c
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the DeleteEmailRow macro.
 
Upvote 0
Welcome to posting at MrExcel!

.. or possibly this non-looping version that also removes all occurrences of a Sheet1 email address, should there happen to be more than one of them in Sheet6 (6).

My assumption is that column C in Sheet6 (6) is available to use as a helper

Rich (BB code):
Sub DelRws()
  Application.ScreenUpdating = False
  With Sheets("Sheet6 (6)")
    .Range("C2").Formula = "=MATCH(B2,Sheet1!A:A,0)"
    With .Range("A1", .Range("B" & Rows.Count).End(xlUp))
      .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=.Cells(1, 3).Resize(2), Unique:=False
      .Offset(1).SpecialCells(xlVisible).EntireRow.Delete
    End With
    On Error Resume Next
    .ShowAllData
    On Error GoTo 0
    .Range("C2").ClearContents
  End With
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,753
Messages
6,132,514
Members
449,732
Latest member
Viva

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top