Error handling a macro to delete a row

Bradzo

New Member
Joined
Nov 15, 2017
Messages
6
Hi all,

I have a problem where I have the below code deletes the upper most empty row if no name or an incorrect name is entered into the InputBox?

I thought I may overcome this with If searchthis = "" Then Exit Sub but it doesn't seem to work. Help would be appreciated.

Thanks.



Sub Button4_Click()
'Archive person X Dock
Dim searchthis

searchthis = InputBox("Enter employee NAME to search.", "Property Search")

If searchthis = "" Then
Exit Sub
End If

On Error GoTo errorhandler

Application.ScreenUpdating = False

ActiveSheet.Range("b:b").Select
Selection.Find(What:=searchthis, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext).Activate
ActiveCell.Select


Rows(ActiveCell.Row).Delete Shift:=xlUp

Application.ScreenUpdating = True

Exit Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,168
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Try using
You also didn't have a error handler for the error line to go to
VBA Code:
Sub Button4_Click()
Dim searchthis As String
On Error GoTo errorhandler
searchthis = InputBox("Enter employee NAME to search.", "Property Search")
If searchthis = "" Then Exit Sub
Application.ScreenUpdating = False
ActiveSheet.Range("b:b").Find(What:=searchthis, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext).Activate
Rows(ActiveCell.Row).Delete
Application.ScreenUpdating = True
Exit Sub
errorhandler:
MsgBox "Nothing found"
Application.ScreenUpdating = True
Exit Sub
End Sub
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,168
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
glad to help... (y) (y)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,994
Messages
5,639,436
Members
417,089
Latest member
jonstr101

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top