Error 1004 - Delete method of range class failed

Rfriend

Board Regular
Joined
May 10, 2021
Messages
73
Office Version
  1. 2010
Platform
  1. Windows
I have code I copied from a tutorial that is no longer available. When I use the code I get the 1004 error "Delete Method of Range Class Failed. I have nailed it down to th this line: findvalue.EntireRow.Delete, but I can't figure out why or how to get passed it. My worksheet is an employee roster. THe search is looking for a record number returned from a search to a list box in a user form. I double click the record and it fills 30 controls in the user form. I should then be able to delete the record selected based on the record number in control "Emp1". The code requires that the position number "Emp4" and record number "Emp1" be present to proceed.

The code is posted below. Any education and resolution is very welcomed at this point.

Thank you

VBA Code:
Private Sub cmdDeleteEmp_Click()
'declare the variables
    Dim findvalue As Range
    Dim cDelete As VbMsgBoxResult
    Dim cNum As Integer
'error statement
    On Error GoTo errHandler:
'check for values
    If Emp1.Value = "" Or Emp4.Value = "" Then
        MsgBox "There is no data to delete"
        Exit Sub
    End If
'give the user a chance to change their mind
    cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "click OK to detete this record")
    If cDelete = vbYes Then
'find the row
        Set findvalue = Sheet7.Range("A:A").Find(What:=Emp1, LookIn:=xlValues)
       findvalue.EntireRow.Delete  'error line
    End If
'clear the controls
    cNum = 30
    For x = 1 To cNum
        Me.Controls("Emp" & x).Value = ""
    Next
'run the filter
    AdvFilterOutdata
'add the values to the listbox
    lstEmployee.RowSource = ""
    lstEmployee.RowSource = "Outdata"
'error block
    On Error GoTo 0
    Exit Sub
errHandler::
    MsgBox "An Error has Occurred  " & vbCrLf & "The error number is:  " _
           & Err.Number & vbCrLf & Err.Description & vbCrLf & _
           "Please notify the administrator"
End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    72.5 KB · Views: 8

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I'm puzzled that we appear to be talking at cross-purposes. Did you try my recommendations in post #10?
 
Upvote 0
I'm puzzled that we appear to be talking at cross-purposes. Did you try my recommendations in post #10?
Thanks for trying to help. At this point I will just junk this and rewrite. To clarify, I am not having a problem searching for anything, my issue is deleting the record (table row) returned to the controls after selecting from the listbox. I guess I am not clear on what you need. Thanks again.
 
Upvote 0
@Rfriend, What happens with the change below?

Rich (BB code):
    If cDelete = vbYes Then
'find the row
        Set findvalue = Sheet7.Range("A:A").Find(What:=CLng(Emp1), LookIn:=xlValues)
        If Not findvalue Is Nothing Then
          findvalue.EntireRow.Delete  'error line
        Else
          MsgBox "Cannot find record " & Emp1.Value
          Exit Sub
       End If
    End If
 
Last edited:
Upvote 0
Back post #2, I posted this recommended code:

VBA Code:
    If cDelete = vbYes Then
'find the row
        Set findvalue = Sheet7.Range("A:A").Find(What:=Emp1, LookIn:=xlValues)
        If Not findvalue Is Nothing Then
          findvalue.EntireRow.Delete  'error line
        Else
          MsgBox "Cannot find record " & Emp1.Value
          Exit Sub
       End If
    End If


Then in post #5 you posted this:

Capture3.PNG


On closer inspection, I see your code contains an error.

You are using:
Rich (BB code):
If findvalue Is Nothing Then


When you should be using
Rich (BB code):
If Not findvalue Is Nothing Then

Try making that change and re-run
 
Upvote 0
Back post #2, I posted this recommended code:

VBA Code:
    If cDelete = vbYes Then
'find the row
        Set findvalue = Sheet7.Range("A:A").Find(What:=Emp1, LookIn:=xlValues)
        If Not findvalue Is Nothing Then
          findvalue.EntireRow.Delete  'error line
        Else
          MsgBox "Cannot find record " & Emp1.Value
          Exit Sub
       End If
    End If


Then in post #5 you posted this:

View attachment 88330

On closer inspection, I see your code contains an error.

You are using:
Rich (BB code):
If findvalue Is Nothing Then


When you should be using
Rich (BB code):
If Not findvalue Is Nothing Then

Try making that change and re-run
 
Upvote 0
Back post #2, I posted this recommended code:

VBA Code:
    If cDelete = vbYes Then
'find the row
        Set findvalue = Sheet7.Range("A:A").Find(What:=Emp1, LookIn:=xlValues)
        If Not findvalue Is Nothing Then
          findvalue.EntireRow.Delete  'error line
        Else
          MsgBox "Cannot find record " & Emp1.Value
          Exit Sub
       End If
    End If


Then in post #5 you posted this:

View attachment 88330

On closer inspection, I see your code contains an error.

You are using:
Rich (BB code):
If findvalue Is Nothing Then


When you should be using
Rich (BB code):
If Not findvalue Is Nothing Then

Try making that change and re-run

I have the correct code but still get the same error in the same place. Emp1 id the record ID selected upon double clicking a selected record. Error msg states it can't find the record number, but the number is there and exists in emp1 control.




Code I am using with your changes:

VBA Code:
Option Explicit
Dim cNum As Integer
Dim x As Integer

Private Sub cmdDeleteEmp_Click()
    Dim findvalue As Range
    Dim cDelete As VbMsgBoxResult
    Dim cNum As Integer
        On Error GoTo cmdDeleteEmp_Error:
    If Emp4.Value = "" Then
        MsgBox "There is no data to delete"
        Exit Sub
    End If
        cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "Click to confirm")
    If cDelete = vbYes Then

    Set findvalue = Sheet7.Range("A:A").Find(What:=Emp1, LookIn:=xlValues)
        If Not findvalue Is Nothing Then
          findvalue.EntireRow.Delete  'error line
        Else
          MsgBox "Cannot find record " & Emp1.Value
        Exit Sub
        End If
    End If
        
'findvalue.EntireRow.Delete
    cNum = 33
        For x = 1 To cNum
            Me.Controls("Emp" & x).Value = ""
        Next
    Unprotect_All
        AdvFilterArchive
        lstEmployee.RowSource = ""
        lstEmployee.RowSource = "OutData"
        On Error GoTo 0
    Protect_All
        Exit Sub
cmdDeleteEmp_Error:
    Protect_All
        MsgBox "Error" & Err.Number & " (" & Err.Description & ") in procedure cmdDeleteEmp _Click of Form EmployeeDB"
End Sub
 

Attachments

  • Capture9.PNG
    Capture9.PNG
    246.1 KB · Views: 5
Upvote 0
1679959831043.png


"<Out of Context>" means the macro was not executing when you took the snapshot. You need to set a breakpoint and run the macro.
 
Upvote 0
Change this
VBA Code:
Set findvalue = ActiveSheet.Range("A:A").Find(What:=Emp1, LookIn:=xlValues)
To this
VBA Code:
Set findvalue = Sheet7.Range("A:A").Find(What:=Emp1.Value, LookIn:=xlValues)
 
Upvote 0

Forum statistics

Threads
1,215,389
Messages
6,124,662
Members
449,178
Latest member
Emilou

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