VBA Delete Row

hanz91

New Member
Joined
Jul 10, 2015
Messages
2
Hi, I am trying to write a code to delete rows based on word in a particular column. I noticed that if the words that should trigger deletion of the row are absent from my file, then I get an error message (Run-time error '91' Object variable or with block variable not set). Is there any way to alter the code so that even when the trigger words are absent, the macro does not crash?

Sub ForeseeuploadCallTypes()


Dim Word(0 To 4) As String
Word(0) = "Uncategorized"
Word(1) = "Merchant Inquiries"
Word(2) = "Autoreply/Spam"
Word(3) = "OB – Confirm Booking"
Word(4) = "OB - Confirm Cancellation"
Dim rDel As Range, U As Range, r As Range
Set rDel = Nothing
Set U = Range("F2:F12000")
For Each r In U
v = r.Value
For i = 0 To 4
If v = Word(i) Then
If rDel Is Nothing Then
Set rDel = r
Else
Set rDel = Union(r, rDel)
End If
End If
Next
Next
rDel.EntireRow.Delete
End Sub


This is a problem because this macro is part of a 'call macro' series and when it crashes it prevents subsequent macros that are part of the series from running.

Really appreciate the help!
Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Assume your error occurs with line rDel.EntireRow.Delete? If so, would this work?
Code:
If Not rDel Is Nothing Then
  rDel.EntireRow.Delete
End If
 
Upvote 0
Ok I have added the code and at least I am not getting an error, but my loop does not go past The first Next>

For Each r In U
v = r.Value
For i = 0 To 15
If v = Word(i) Then
If rDel Is Nothing Then
Set rDel = r
Else
Set rDel = Union(r, rDel)
End If
End If
Next '<--------------------
Next
If Not rDel Is Nothing Then
rDel.EntireRow.Delete
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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