The function below works if it finds 'NAME'. However, if 'NAME' is not found I get an error message and I haven't been able to figure out how to stop the function if there is an error.
Thank you
John
Sub Test_Click() Dim Name As String Name = Range("M11") Sheets("Data").Select Range("N:N").Select 'On Error Resume Next 'Exit Sub Selection.Find(what:=Name, After:=ActiveCell, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False).Activate Rows(ActiveCell.Row).Select Selection.Delete Shift:=xlUp End Sub
Last edited by jwichern1; 03-03-2011 at 03:41 PM.
Assign the result of the search to a range variable and test if there is anything there:Sub Test_Click() Dim Name As String, r As Range Name = Range("M11") Set r = Sheets("Data").Range("N:N").Find(what:=Name, After:=ActiveCell, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False) If Not r Is Nothing Then Rows(r.Row).Delete Shift:=xlUp End If End Sub
Thank you for your quick response. However I am getting a Run-Time error '13': Type Mismatch. Any suggestions how to correct this?
On which line? Does this work?
Set r = Sheets("Data").Range("N:N").Find(what:=Name, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False)
It kind-of works, but when the code is run it deletes the row where the 'NAME' was defined, which is a different sheet. I would like it to delete the row on the data sheet where it found the match.
I would have thought it should do that. This is where you attach a workbook.
I added the code below after defining 'NAME' and that seems to have corrected:
So full code reads:Sheets("Data").Select
Sub Test_Click() Dim Name As String, r As Range Name = Range("M11") Sheets("Data").Select Set r = Sheets("Data").Range("N:N").Find(what:=Name, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False) If Not r Is Nothing Then Rows(r.Row).Delete Shift:=xlUp End If End Sub
Rows(r.Row).Delete Shift:=xlUp
Your problem was stemming from this line. You were seaching for the row and your data sheet but THEN the line above was telling it to remove the Row on the currenlty active sheet.
Here's this line corrected so that it removes from the data sheet ... even if the data sheet is not the active sheet.
Sub Test_Click() Dim Name As String, r As Range Name = ActiveSheet.Range("M11") Set r = Sheets("Data").Range("N:N").Find(what:=Name, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False) If Not r Is Nothing Then r.EntireRow.Delete Shift:=xlUp End If End Sub
Sorry, wasn't paying attention. Either a sheet reference or nimrod's approach will sort it out.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks