Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Macro

Status
Not open for further replies.

Netwrkengeer

IS-IT--Management
Apr 4, 2001
184
0
0
US
I need a macro that will delete all data except email addresses, the data may be a cell full of text then an email address in the middle of the text. this is what I have... The problem with this macro is it deletes the columns and I need it to delete all data, other then the email address, Also I would like it to delete duplicate email addresses.

[ Sub ExtractAddresses()
Application.ScreenUpdating = False

Range("A65536").Select
ActiveCell.End(xlUp).Select
x = ActiveCell.Offset(1, 0).Address
intCounter = 1

Range("B1").Select

Columns("A:A").Select
Selection.Find(What:="Contact: E-mail ", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
x = ActiveCell.Address
ActiveCell.Offset(0, 1).Value = "%"
x = ActiveCell.Address
Selection.FindNext(After:=ActiveCell).Activate

Do Until ActiveCell.Address = x
ActiveCell.Offset(0, 1).Value = "%"
Selection.FindNext(After:=ActiveCell).Activate
intCounter = intCounter + 1
Loop

Range("B1").Select

Do Until intCounter = 0
If ActiveCell.Value <> &quot;%&quot; Then
ActiveCell.EntireRow.Delete

Else
ActiveCell.Offset(1, 0).Select
intCounter = intCounter - 1
End If
Loop

Columns(&quot;B:B&quot;).Select
Selection.Delete
Range(&quot;A1&quot;).Select


End Sub ]

Thanks
 
If this is a one-time data cleanup, you don't need VBA.

1. Assuming your data is in column A, starting in row one, put these formulas/data in A1 thru E1:
Code:
A1: 'data Contact: E-mail rstuve@abc.com more data
B1: =FIND(&quot;Contact: E-mail &quot;,A1)
C1: =IF(ISERROR(B1),&quot;&quot;,MID(A1,B1+16,99))
D1: =IF(C1=&quot;&quot;,&quot;&quot;,FIND(&quot; &quot;,C1&&quot; &quot;))
E1: =IF(D1=&quot;&quot;,&quot;%&quot;,LEFT(C1,D1-1))
2. Copy the formulas down as far as you have data.
3. Select column E, copy and paste special values.
4. Delete columns A thru D
5. Sort the worksheet
6. Use Data/Filter/Advanced Filter... to extract unique records. (You may need to insert row with a heading first to satisfy Excel's requirements.)
7. Delete the row where the only entry is &quot;%&quot;

If you still need to do this in VBA, please provide a little more details in the way of sample data so it can be tailored for you.




 
Here's another way. If the source file is a text file.

(rough psuedo code, bare with me.)

open &quot;c:\sourcefile.txt&quot; for input as #1
open &quot;c:\outputfile.txt&quot; for output as #2
do while not EOF(1)

input #1, strLine

if instr(1,&quot;Contact EMAIL&quot;,vbtextcompare)
' strip out excess data here

write #2,strline

loop

close #1
close #2

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top