Sorry took so long had some things to take care of. Let me know if you have any problems
You will need to update the Const at the top of the Sub. sFilePath to the Path of the file. sFileName to the name of the file. shPhysicians to the Name of the sheet that has the Physicians Names.
Sub abc()
Const sFilePath As String = "C:\Users\Mike\Desktop\" ' File Path must have "\" at the end path
Const sFileName As String = "PhysiciansList.xls" ' File Name
Const shPhysicians As String = "Names" ' Sheet name from PhysiciansList Workbook
Dim ws As Worksheet
Dim FoundCell As Range
Dim RangeToDelete As Range
Dim drNames As Variant
Dim i As Long, LastRow As Long
Dim LastCell As Range
Dim FirstAddr As String
Dim xlApp As New Excel.Application
Dim xlWb As Workbook
On Error Resume Next
Set xlWb = xlApp.Workbooks.Open(sFilePath & sFileName, ReadOnly:=True)
If Err.Number <> 0 Then
MsgBox "Error #:" & Err.Number & vbCrLf & "Description:" & Err.Description, vbCritical, "Error"
Set xlApp = Nothing
Exit Sub
End If
With xlWb.Worksheets(shPhysicians)
drNames = .Range("a1:a" & .Cells(Rows.Count, "a").End(xlUp).Row)
End With
If Err.Number <> 0 Then
MsgBox "Could not find Sheet name: " & shPhysicians, vbCritical, "Error"
xlWb.Close
xlApp.Quit
Set xlApp = Nothing
Set xlWb = Nothing
Exit Sub
End If
On Error GoTo 0
xlWb.Close
xlApp.Quit
Set xlApp = Nothing
Set xlWb = Nothing
Set ws = Worksheets("sheet2")
With ws
LastRow = .Cells(Rows.Count, "f").End(xlUp).Row
With .Range("f2:f" & LastRow)
Set LastCell = .Cells(.Cells.Count)
End With
For i = LBound(drNames) To UBound(drNames)
Set FoundCell = .Range("f2:f" & LastRow).Find(What:=drNames(i, 1), LookAt:=xlWhole)
If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
If RangeToDelete Is Nothing Then
Set RangeToDelete = .Cells(FoundCell.Row, "a")
Else
Set RangeToDelete = Union(RangeToDelete, .Cells(FoundCell.Row, "a"))
End If
Set FoundCell = .Range("f2:f" & LastRow).FindNext(After:=FoundCell)
If FoundCell.Address = FirstAddr Then
Exit Do
End If
Loop
Next i
If Not RangeToDelete Is Nothing Then
RangeToDelete.EntireRow.Delete
End If
End With
Set ws = Nothing
Set FoundCell = Nothing
Set RangeToDelete = Nothing
End Sub
Bookmarks