How to Delete all emails in adjacent cells that come from the same domain. I only want to keep 1st record from a given domain

jodphd

New Member
Joined
Mar 11, 2022
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I am using Excel 2016. I have a spreadsheet with a column of email addresses where some adjacent cells are from other people at a company and I only want to keep the first email address from each domain and delete any subsequent email addresses from the same domain. Not all adjacent records will have the same domain--some could be from a new domain that I want to keep.

Example: here is original column view:

Here is what I want corrected column to look like:



The spreadsheet will only have 1 column in it---just email addresses. There will be a lot of records so I ma looking for a way to automate this. Thanks in advance.
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You might consider the following...

VBA Code:
Sub UniqueDomains()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim r As Range
Dim LastRow As Long, i As Long
Dim arr() As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set ws1 = ActiveSheet
ws1.Copy after:=ws1
Set ws2 = ActiveSheet

LastRow = ws2.Cells(Rows.Count, "A").End(xlUp).Row
For Each r In ws2.Range("A2:A" & LastRow)
    arr = Split(r, "@")
    r.Offset(0, 1) = arr(0)
    r.Offset(0, 2) = arr(1)
Next r

'ws2.Range("A2:C" & LastRow).Sort key1:=Range("C2")
For i = LastRow To 2 Step -1
    If ws2.Cells(i, 3) = ws2.Cells(i + 1, 3) Then ws2.Rows(i + 1).EntireRow.Delete
    ws2.Cells(i, 2) = ws2.Cells(i, 2) & "@" & ws2.Cells(i, 3)
Next i
ws2.Range("A2:A" & LastRow).Copy Destination:=ws1.Range("B2")
ws2.Delete
ws1.Activate
Columns(2).AutoFit

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Cheers,

Tony
 
Upvote 0
Hi and welcome to MrExcel!

Here is another macro for you to consider.
The results in column B

VBA Code:
Sub Delete_emails()
  Dim dic As Object, a As Variant, i As Long, domain As String
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  Set dic = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    domain = Split(a(i, 1), "@")(1)
    If Not dic.exists(domain) Then dic(domain) = a(i, 1)
  Next
  Range("B2").Resize(dic.Count).Value = Application.Transpose(dic.items)
End Sub
---
HOW TO INSTALL MACROs
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (Delete_emails) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.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.
 
Upvote 0
Hi,

Formula option, Filter or Delete blank rows as you like:

Book3.xlsx
AB
1Original DataCorrected Data
2test@yahoo.comtest@yahoo.com
3john@doe.comjohn@doe.com
4richard@doe.com 
5joe@msn.comjoe@msn.com
6test@hotmail.comtest@hotmail.com
7january@hotmail.com 
8ron@arizona.netron@arizona.net
Sheet1045
Cell Formulas
RangeFormula
B2:B8B2=IF(SUMPRODUCT(--ISNUMBER(SEARCH("*"&MID(A2,FIND("@",A2),99),A$2:A2)))=1,A2,"")
 
Upvote 0
Solution
Another macro approach

VBA Code:
Sub OnePerDomain()
  With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=vlookup(replace(A2,1,find(""@"",A2)-1,""*"")," & .Offset(, -1).Address & ",1,0)"
    .Value = .Value
    .RemoveDuplicates Columns:=1, Header:=xlNo
  End With
End Sub


.. and another formula approach

jodphd.xlsm
AB
1Original DataCorrected Data
2test@yahoo.comtest@yahoo.com
3john@doe.comjohn@doe.com
4richard@doe.comjoe@msn.com
5joe@msn.comtest@hotmail.com
6test@hotmail.comron@arizona.net
7january@hotmail.com 
8ron@arizona.net 
Formula
Cell Formulas
RangeFormula
B2:B8B2=IFNA(INDEX($A$2:$A$100,MATCH(0,INDEX(COUNTIF($B$1:B1,REPLACE($A$2:$A$100,1,FIND("@",A$2:A$100)-1,"*"))+(A$2:A$100=""),0),0)),"")
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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