VBA - Searching a value and deleting a row if found

PaulFletcher

New Member
Joined
Oct 7, 2016
Messages
13
Hi,

I have written the below code to search a value and if found remove the entire row and if successful pop a message advising the record has been deleted.

I have two problems:
1. When there is no data the message will still pop up confirming something has been deleted which isn't the case. When there is data the message pop's up twice?
2. I have attempted to add further code using "Else" to pop a further message advising if a record does not exist but also cannot get this to work.


With .Cells(Lrow, "A")

If Not IsError(.Value) Then

If .Value = ThisWorkbook.Sheets("Input").Range("B21") Then .EntireRow.Delete
MsgBox "Record Deleted"


End If

End With


Any help would be much appreciated.

Thanks

Paul
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Suggest you tell us in simple English what you are attempting to do if you were to do this manually and then we can provide you with some efficient code.
 
Upvote 0
Thanks Alan.

I am attempting to search a value and if it exists remove it and the row it is in from a separate worksheet.
If this is successful a message box will appear confirming thios has been deleted
If this is unsuccessful (i.e. the value does not exist) a message will appear stating the record is not found.

Hope that makes sense...

Thanks

Paul
 
Upvote 0
hi,
not showing all your code but as a guess try:

Code:
     Dim FoundCell As Range    
     Dim Search As String


    Search = ThisWorkbook.Sheets("Input").Range("B21").Value
    
    Set FoundCell = Columns(1).Find(Search, lookat:=xlWhole, LookIn:=xlValues)
    If Not FoundCell Is Nothing Then
        FoundCell.EntireRow.Delete
        MsgBox Search & Chr(10) & "Record Deleted", 48, "Record Deleted"
    Else
        MsgBox Search & Chr(10) & "Record Not Found", 48, "Not Found"
    End If

Dave
 
Last edited:
Upvote 0
Thanks Dave

I have tried this and have successfully popped a message box if tghe record is deleted or not; however if it is deleted it pops both messages?
I have put my code in full which i apologise for a\s has been taken from a few separate sources so probably isn;t perfect by any means!

Code:
Sub SearchBookings()


Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    
    Dim FoundCell As Range
    Dim Search As String
     
    Set bookings = Worksheets("Bookings Data")
    Set inputdata = Worksheets("Input")
    
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With


    With bookings
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row


        For Lrow = Lastrow To Firstrow Step -1


            Search = ThisWorkbook.Sheets("Input").Range("B21").Value
    
        Set FoundCell = Columns(1).Find(Search, lookat:=xlWhole, LookIn:=xlValues)
             If Not FoundCell Is Nothing Then
            FoundCell.EntireRow.Delete
            MsgBox "Record Deleted"
        Else
            MsgBox Search & Chr(10) & "Record Not Found", 48, "Not Found"
            End If
                               
    
     
        Next Lrow


       ActiveWindow.View = ViewMode
    
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With




   
            With Sheets("Input").Select
                    Range("B21").Select
                    Selection.ClearContents
            End With
    
End With
End Sub
 
Upvote 0
Hi,

you do not need to place code in For next Loop

Rich (BB code):
  For Lrow = Lastrow To Firstrow Step -1




        Search = ThisWorkbook.Sheets("Input").Range("B21").Value
    
        Set FoundCell = Columns(1).Find(Search, lookat:=xlWhole, LookIn:=xlValues)
             If Not FoundCell Is Nothing Then
            FoundCell.EntireRow.Delete
            MsgBox "Record Deleted"
        Else
            MsgBox Search & Chr(10) & "Record Not Found", 48, "Not Found"
            End If
                               
    
     
 Next Lrow

your values are not applicable to my suggestion - delete all code shown in RED

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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