MoOsEBoT88
New Member
- Joined
- Sep 13, 2011
- Messages
- 15
- Office Version
- 365
- Platform
- Windows
I want to be able to delete a row based on Cell B3, B5 selects which worksheet to delete the row off.
It also deletes the row of a central search sheet.
Here's the code I currently have but I get an 424: Object Required Error
I'm not fluent in VB, just copied and pasted from previous code snippets.
Hope you can spot my errors.
It also deletes the row of a central search sheet.
Here's the code I currently have but I get an 424: Object Required Error
Code:
Sub DeleteRecord()
Application.ScreenUpdating = True
Application.Run "UnlockSheet"
Dim I As Integer
On Error GoTo validateError
If Range("E4") = "NOREC" Then Range("B3").Select: Err.Raise 513
If MsgBox("Do you want to delete this record", vbQuestion + vbYesNo) = vbYes Then
Dim strName As String
strName = Range("B5")
Sheets(strName).Activate
Application.Run "UnlockSheet"
'Deletes Row in Worksheet
Dim LastRow As Long
Dim rng As Range
With ActiveWorksheet
Columns("A:B").ColumnWidth = 9.71
ActiveWorksheet.Rows(1).Insert
ActiveWorksheet.Range("A1").Value = "Temp"
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = .Range("A1").Resize(LastRow)
rng.AutoFilter Field:=1, Criteria1:="=" & Worksheets("DeleteEdit").Range("B3").Value
On Error Resume Next
Set rng = rng.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then rng.EntireRow.Delete
End With
'Resize column back to 0
Columns("A:B").ColumnWidth = 0
'Deletes from search
Dim LastRow2 As Long
Dim rng2 As Range
With Worksheets("Search")
.Rows(1).Insert
.Range("A1").Value = "Temp"
LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng2 = .Range("A1").Resize(LastRow2)
rng2.AutoFilter Field:=1, Criteria1:="=" & Worksheets("DeleteEdit").Range("B3").Value
On Error Resume Next
Set rng2 = rng2.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng2 Is Nothing Then rng2.EntireRow.Delete
End With
'Clear B3 Contents
Application.Run "ClearDelete"
MsgBox "Record Deleted", VbMsgBoxStye = vbOKOnly, Completed
Application.Run "DeleteAnother"
Else
MsgBox "Operation Cancelled", VbMsgBoxStye = vbOKOnly, Cancelled
End If
Application.ScreenUpdating = True
Application.Run "LockSheet"
Exit Sub
Hope you can spot my errors.
Last edited: