VBA - find then delete data/row

george hart

Board Regular
Joined
Dec 4, 2008
Messages
241
Hi

The code below works fine, in that you can enter data and it will find it and delete the rows. However, it falls over when you enter data that isn't there.

Any way I can get a message pop up when this happens to say data not found and then end sub??

Dim Answer As String
Answer = MsgBox("Are you sure you want to remove a KPI?", vbYesNo)
If Answer <> vbYes Then Exit Sub
data = (InputBox("Enter the KPI you want to remove"))
Sheets(Array("Actual Operations KPI", "Targets Operations KPI", _
"Data provider - Definition")).Select
Sheets("Actual Operations KPI").Activate
Cells.Find(What:=(data), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.EntireRow.Delete
Sheets("Current Period-YTD Ops KPI").Select
Cells.FindNext(After:=ActiveCell).Activate
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlUp
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could try an error handler. Maybe something like so:

Code:
Public Sub Test()
 
Dim Answer As String
 
On Error GoTo MyError
 
Answer = MsgBox("Are you sure you want to remove a KPI?", vbYesNo)
If Answer <> vbYes Then Exit Sub
Data = (InputBox("Enter the KPI you want to remove"))
Sheets(Array("Actual Operations KPI", "Targets Operations KPI", _
"Data provider - Definition")).Select
Sheets("Actual Operations KPI").Activate
Cells.Find(What:=(Data), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.EntireRow.Delete
Sheets("Current Period-YTD Ops KPI").Select
Cells.FindNext(After:=ActiveCell).Activate
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlUp
 
Exit Sub
 
MyError:
 
MsgBox "Data not found"
 
End Sub
Gary
 
Upvote 0
Generally it's a good idea to set a reference to a found cell. I.e.:
Code:
Dim rngFoundCell as Range

On Error Resume Next
    set rngFoundCell = Cells.Find(What:=myString, LookAt:=xlWhole)
On Error GoTo 0
Then there will be instances where the range object variable is Nothing (i.e. the search string was not found). You should check if it is Nothing before invoking the rest of the code. I.e:
Code:
If Not rngFoundCell is Nothing Then
    'do something
End If

I don't believe you should mask the errors throughout your code...
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

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
Back
Top