Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to search, compare, copy and delete

Hi everyone,

I am trying to figure out a way to easily do this task.

I have an Excel file containing a list of people. This list is i
"Sheet1"
The headers for each column in this list a
Column A
Dept. Name

Column B
Last Name

Column C
First Name

Column D
Email Address

Column E
Newtork ID

I have another list in "Sheet2"
The headers for each column in this list a
Column A
Name

The names in this column are formatted as "First name space Last Name"
for instance, "John Doe" .

What I want to do is compare the names in Column A on "Sheet2" wit
Column B and Column C in "Sheet1"
If they are the same name, then copy Column A, D and E into "Sheet2
and remove the row containing matching the name in "Sheet1"

Thanks in advance for any help.

Mar

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 218
Default Macro to search, compare, copy and delete

Suggested is the following:

Sub TransferAndDelete()
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range
Dim C1 As Range, C2 As Range, C3 As Range
Dim FNm As String, LNm As String

With Sheets("Sheet2")
Set Rng1 = .Range("A2:A" & .UsedRange.Rows.Count)
'Next line only necessary if gap in data possible
Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
End With
With Sheets("Sheet1")
Set Rng2 = .Range("B2:B" & .UsedRange.Rows.Count)
'Next line only necessary if gap in data possible
Set Rng2 = Rng2.SpecialCells(xlCellTypeConstants)
Set Rng3 = Rng2.Offset(, 1)
End With
For Each C1 In Rng1.Cells
C1 = Trim(C1)
If InStr(2, C1, " ") 0 Then
FNm = Left(C1, InStr(1, C1, " ") - 1)
LNm = Right(C1, Len(C1) - Len(FNm) - 1)
Else
FNm = ""
LNm = ""
End If
For Each C2 In Rng2.Cells
C2 = Trim(C2)
If C2 = LNm Then
Set C3 = C2(1, 2)
If Trim(C3) = FNm Then
C1(1, 2) = C2(1, 0)
C1(1, 3) = C3(1, 2)
C1(1, 4) = C3(1, 3)
C2.EntireRow.EntireRow.Delete
End If
End If
Next
Next
End Sub

Minimal testing done.

Regards,
Greg
(VBA Amateur)

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 218
Default Macro to search, compare, copy and delete

Correction to the line:
C2.EntireRow.EntireRow.Delete
Should be
C2.EntireRow.Delete

Don't know how that happened ???

Regards,
Greg
-----Original Message-----
Suggested is the following:

Sub TransferAndDelete()
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range
Dim C1 As Range, C2 As Range, C3 As Range
Dim FNm As String, LNm As String

With Sheets("Sheet2")
Set Rng1 = .Range("A2:A" & .UsedRange.Rows.Count)
'Next line only necessary if gap in data possible
Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
End With
With Sheets("Sheet1")
Set Rng2 = .Range("B2:B" & .UsedRange.Rows.Count)
'Next line only necessary if gap in data possible
Set Rng2 = Rng2.SpecialCells(xlCellTypeConstants)
Set Rng3 = Rng2.Offset(, 1)
End With
For Each C1 In Rng1.Cells
C1 = Trim(C1)
If InStr(2, C1, " ") 0 Then
FNm = Left(C1, InStr(1, C1, " ") - 1)
LNm = Right(C1, Len(C1) - Len(FNm) - 1)
Else
FNm = ""
LNm = ""
End If
For Each C2 In Rng2.Cells
C2 = Trim(C2)
If C2 = LNm Then
Set C3 = C2(1, 2)
If Trim(C3) = FNm Then
C1(1, 2) = C2(1, 0)
C1(1, 3) = C3(1, 2)
C1(1, 4) = C3(1, 3)
C2.EntireRow.EntireRow.Delete
End If
End If
Next
Next
End Sub

Minimal testing done.

Regards,
Greg
(VBA Amateur)

.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search / Compare / Copy Value Up (cpm) sandy_eggo Excel Discussion (Misc queries) 1 February 11th 09 05:27 PM
Macro to Search and Copy davidnagel Excel Discussion (Misc queries) 4 July 29th 08 10:13 AM
Search, Copy, Paste Macro in Excel [email protected] Excel Worksheet Functions 0 January 3rd 06 06:51 PM
search/copy/paste macro M Excel Programming 1 December 29th 03 07:26 PM
Search/copy/paste Macro mjwillyone Excel Programming 2 December 27th 03 07:49 AM


All times are GMT +1. The time now is 11:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"