+ Reply to Thread
Results 1 to 28 of 28

Excel Macro to delete row if one of 37 plus names is found

Hybrid View

  1. #1
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Excel Macro to delete row if one of 37 plus names is found

    sample data.xlsHello Everyone!

    I need help to create a macro to delete a row if a certain name is found in column F of sheet2. The problem is that there are over 37 names and the name format is Last name, First name the middle initial. A few of the names are even more challenging. Below are a few examples of the name format:

    Rusk, Debra L
    O'Donnell, Daniel P
    Palmer-Smith, Victoria C
    Weinstein, Elizabeth
    Sequera Sanchez, Ivan D
    Last edited by johnny04; 09-17-2012 at 11:06 AM.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    it would be nice or better I should say is to upload a workbook with the data and show what rows would be deleted
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Thank you Mike for replying. Unfortunately I can not upload the workbook due to patient information that it contains. Also the workbook does not contain all of the medical physician’s names that may appear in the future. Each day the report has different names.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    You can just mock up and example with some data and the challenging data, Show how it excist on bothe sheets.

  5. #5
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Okay, I have added the sample data to my first post. I have just added a few rows. The original has well over 3,000 rows. Let me know if there is anything else that you need. Thank you for helping me.

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    Ok see if this will do what you need?

    Option Explicit
    Sub abc()
    Dim FoundCell As Range, RangeToDelete As Range
    Dim i As Long
    Dim a As Variant
    Dim ws1 As Worksheet
        
        
        With Worksheets("Sheet2")
            a = .Range("f2:f" & .Cells(Rows.Count, "f").End(xlUp).Row)
        End With
        
        Set ws1 = Worksheets("sheet1")
        With ws1
            For i = 1 To UBound(a)
                Set FoundCell = .Range("f:f").Find(What:=a(i, 1), LookAt:=xlWhole)
                If Not FoundCell Is Nothing Then
                    If RangeToDelete Is Nothing Then
                        Set RangeToDelete = .Cells(FoundCell.Row, "a")
                    Else
                        Set RangeToDelete = Union(RangeToDelete, .Cells(FoundCell.Row, "a"))
                    End If
                End If
            Next i
            If Not RangeToDelete Is Nothing Then
                RangeToDelete.EntireRow.Delete
            End If
        End With
        Set ws1 = Nothing
        Set FoundCell = Nothing
        Set RangeToDelete = Nothing
    End Sub

  7. #7
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Mike, each sheet contains needed but different information. Basically I will copy and paste data from sheet 1 into sheet 2. From there I need to delete the rows based on a particular physicians name. So I do not have a range of names to reference and these reports change from day to day so the above macro will not work.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    So you just want to delete 1 physicians Name from sheet1. You lost me? So working with your sample sheets what is to happen? Are you wanting to be able to select a name on sheet 1 and look on sheet 2 if that name is there, delete it from sheet1?

  9. #9
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Basically only working from sheet2 there are over a 100 different kinds of physician names. Out of that I want to delete 37 or more specific ones and the row that is associated with that physician's name. So I need a macro that searches column "F" for names Rusk, Debra L, O'Donnell, Daniel P, Palmer-Smith, Victoria C, Weinstein, Elizabeth, Sequera Sanchez, Ivan D, ....ect (long list of names) and if found then delete entire row. Does that make sense? Sorry for the poor explanation

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    So the deleting of rows is to be done on sheet2?

    Out of that I want to delete 37 or more specific ones
    Where is the Specific list located? Is my macro just backwards? Should I be deleting rows on sheet2 where there names are on sheet 1?

  11. #11
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    There is no list, in the excel document does there need to be one? I tried creating an array macro but it would not work with a long list of names or the format of some of the longer names such as the ones with a ' in it. An employee runs reports out of another system and places them into the excel spreadsheet. She will do this each day but she does not need all of the physicians so that is why I wanted to delete them. Does that make sense?

    'Deleting ED physicians'
        
        Dim rng As Range
        Dim calcmode As Long
        Dim myArr As Variant
        Dim I As Long
        
        myArr = Array(“Acciani”, “Bartkus”, “Beckman”, “Boe”,....etc)    For I = LBound(myArr) To UBound(myArr)
        With Sheets("Sheet 2")
            .AutoFilterMode = False
                .Range("F2:F" & .Rows.Count).AutoFilter Field:=1, Criteria1:=myArr(I)
                    Set rng = Nothing
                With .AutoFilter.Range
                    On Error Resume Next
                    Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                              .SpecialCells(xlCellTypeVisible)
                    On Error GoTo 0
                    If Not rng Is Nothing Then rng.EntireRow.Delete
                End With
            .AutoFilterMode = False
            End With
        Next I
        With Application
            End With

  12. #12
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    See if this will work. I would have a list of names somewhere so that I didnt have to mess with the code anytime I wanted to make changes to the names

    Option Explicit
    Sub abc()
    Dim ws As Worksheet
    Dim FoundCell As Range
    Dim RangeToDelete As Range
    Dim drNames As Variant
    Dim i As Long
        
        
        'Dr names in column a on some sheeet
        'If you want to use this way uncomment the next 3 lines
        'With Worksheets("somesheet")
        '    drNames = .Range("a1:a" & .Cells(Rows.Count, "a").End(xlUp).Row)
        'End With
        
        ' Add more names
        'If you use the above method comment out the line below
        drNames = Array("O'Donnell, Daniel P", "Palmer-Smith, Victoria C")
    
        Set ws = Worksheets("sheet2")
        With ws
            For i = LBound(drNames) To UBound(drNames)
                Set FoundCell = .Range("f:f").Find(What:=drNames(i), LookAt:=xlWhole)
                If Not FoundCell Is Nothing Then
                    If RangeToDelete Is Nothing Then
                        Set RangeToDelete = .Cells(FoundCell.Row, "a")
                    Else
                        Set RangeToDelete = Union(RangeToDelete, .Cells(FoundCell.Row, "a"))
                    End If
                End If
            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

  13. #13
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Yeah I think that might be the best bet. That way I will be able to add/take away physicians when needed. Is there a way to have the physician list in another workbook title "ED Physicians Names" and have the macro look for the list on that workbook and not from a different sheet?

  14. #14
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    So did that code work what you were wanting? Yea there is a way. how many times will you be updating the list? If not that much why not just hide the sheet? Or from the VbaProject Window select the sheet and in the Properties set visible to xlsheetveryhidden if you dont want anyone to access the sheet.

  15. #15
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    This code did not work. What did I do wrong?

    Sub abc()
    Dim ws As Worksheet
    Dim FoundCell As Range
    Dim RangeToDelete As Range
    Dim drNames As Variant
    Dim i As Long
        
        
        With Worksheets("sheet3")
            drNames = .Range("a1:a" & .Cells(Rows.Count, "a").End(xlUp).Row)
        End With
    
        Set ws = Worksheets("sheet2")
        With ws
            For i = LBound(drNames) To UBound(drNames)
                Set FoundCell = .Range("f:f").Find(What:=drNames(i), LookAt:=xlWhole)
                If Not FoundCell Is Nothing Then
                    If RangeToDelete Is Nothing Then
                        Set RangeToDelete = .Cells(FoundCell.Row, "a")
                    Else
                        Set RangeToDelete = Union(RangeToDelete, .Cells(FoundCell.Row, "a"))
                    End If
                End If
            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

  16. #16
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    Also will the Doctor that you want to delete, Name be listed more then Once? If so will I will need to update the code.

  17. #17
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    Opps that was my bad I should off set that up to in case you wanted to work with a worksheet. Being were working/setting the array with a range in becomes a 2 dimensional array.

    So change drNames(i) to drNames(i,1) .

    Set FoundCell = .Range("f:f").Find(What:=drNames(i,1), LookAt:=xlWhole)

  18. #18
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Ok thanks but now I got a run-time error '9' subscript out of range on that same line of code. Not sure why though. I did a direct copy and paste of the physicians name on sheet3.

  19. #19
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Yes the physician's name may appear over 5 times depending on how bad they are at documentation

  20. #20
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    This works for me.

    sample data (1).xls

  21. #21
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    It worked for me too but it did not delete all of them.
    Also will the Doctor that you want to delete, Name be listed more then Once? If so will I will need to update the code.
    yes there are multiple rows with the same physician.

  22. #22
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    Heres the workbook to delete more then one

    sample data (2).xls

  23. #23
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    Yea I didnt get you response to there would be more Dr names before I posted the workbook. This code below should do what you need.

    Sub abc()
    Dim ws As Worksheet
    Dim FoundCell As Range
    Dim RangeToDelete As Range
    Dim drNames As Variant
    Dim i As Long
    Dim LastCell As Range
    Dim FirstAddr As String
    
        With Worksheets("sheet3")
            drNames = .Range("a1:a" & .Cells(Rows.Count, "a").End(xlUp).Row)
        End With
    
        Set ws = Worksheets("sheet2")
        With ws
            With .Range("f:f")
                Set LastCell = .Cells(.Cells.Count)
            End With
            For i = LBound(drNames) To UBound(drNames)
                Set FoundCell = .Range("f:f").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("f:f").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

  24. #24
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    for some reason it didn't loop. It still just took out just one name

  25. #25
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    Nevermind that second one worked perfectly!! Can you make a slight modification to have the macro look to a different spreadsheet for the phsician list? This way any user will be able to run the macro. We have more then one person that will be using this macro. I really thank you for your great help!

  26. #26
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    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
    Last edited by mike7952; 09-17-2012 at 03:18 PM.

  27. #27
    Registered User
    Join Date
    09-11-2012
    Location
    indianapolis, Indiana
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Excel Macro to delete row if one of 37 plus names is found

    haha!! success! Thank you very much for taking all this time to help me and work on this I really appreciate it a great deal. Is there any way for me to give you credit for this?

  28. #28
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Excel Macro to delete row if one of 37 plus names is found

    Click Thread Tools above your first post, select "Mark your thread as Solved". Also can click the star under my last post and leave a comment.
    Also might want to recopy the code I made a change to it as soon I hit save and you may copied it before I updated it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1