Hi,
I am trying to search a column of a spreadsheet for a value that I have entered in a text box on a form. If any of the cells within my range contains this value I need to delete the entire row. I made many attempts but each time I get a message
Run time error
Delete method of range class failed.
The worksheet I have referenced is correct and I have run various checks to ensure that the correct RowNo is being picked up, yet still the delete method (or the ClearContents method) isn't working. Can anyone see where I am going wrong with this code. I have posted two of my attempts below.
Any help appreciated
Private Sub DeleteID()
Dim cell As Range
Dim search As Range
Dim projectID As String
Dim Selection As Range
Dim RowNo As Integer
projectID = TextBox6.Text
Set Selection = Worksheets(4).Range("A2:A300"'A1 is header
For Each cell In Selection
If cell.Text = projectID Then
RowNo = cell.Row
Set search = Worksheets(4).Range("A" & RowNo) search.EntireRow.Delete
End If
Next cell
End Sub
Or
Private Sub DeleteID()
Dim cell As Range
Dim projectID As String
Dim Selection As Range
projectID = TextBox6.Text
Set Selection = Worksheets(4).Range("A2:A300"
For Each cell In Selection
If cell.Text = projectID Then
cell.EntireRow.Delete
End If
Next cell
End Sub
I am trying to search a column of a spreadsheet for a value that I have entered in a text box on a form. If any of the cells within my range contains this value I need to delete the entire row. I made many attempts but each time I get a message
Run time error
Delete method of range class failed.
The worksheet I have referenced is correct and I have run various checks to ensure that the correct RowNo is being picked up, yet still the delete method (or the ClearContents method) isn't working. Can anyone see where I am going wrong with this code. I have posted two of my attempts below.
Any help appreciated
Private Sub DeleteID()
Dim cell As Range
Dim search As Range
Dim projectID As String
Dim Selection As Range
Dim RowNo As Integer
projectID = TextBox6.Text
Set Selection = Worksheets(4).Range("A2:A300"'A1 is header
For Each cell In Selection
If cell.Text = projectID Then
RowNo = cell.Row
Set search = Worksheets(4).Range("A" & RowNo) search.EntireRow.Delete
End If
Next cell
End Sub
Or
Private Sub DeleteID()
Dim cell As Range
Dim projectID As String
Dim Selection As Range
projectID = TextBox6.Text
Set Selection = Worksheets(4).Range("A2:A300"
For Each cell In Selection
If cell.Text = projectID Then
cell.EntireRow.Delete
End If
Next cell
End Sub