Run Time Error 91 - VBA is Wrong, I'm Right in Ignoring it...or Am I?

Tash Point O

New Member
Joined
Feb 12, 2018
Messages
47
Hi,

I'm back for the 2nd day in a row seeking help :(. I'm certain the Run Time Error 91 I am receiving while trying to create a loop is VBA just being prissy. I used a nifty error handling code that a member gave me yesterday and I was about to bid the error good riddance but now my OCD is making me want to solve the problem.

Let me have it if I'm wrong and VBA is right in pointing out an error:


1572jdk.jpg
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Care to share what you're actually trying to achieve? Dave

Yes!
In Range A:A, I’d like to find a certain word found in certain cells and once found, clear contents for that entire row. Since there are multiple instances of these cells that contain this wors, I need to loop (or perhaps there’s a better method). My code works except for the pesky error message that i get when i loop.
 
Last edited:
Upvote 0
This would be far quicker....UNTESTED...no selecting required

Code:
Sub MM1()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp)) '.Offset(, 5)
    .Replace "Deal" & Chr(10) & "ID", "#N/A", xlWhole, , False, , False, False
    Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.ClearContents
  End With
End Sub
 
Upvote 0
Try changing this
Code:
.Replace "Deal" & Chr(10) & "ID", "#N/A", [COLOR=#ff0000]xlPart[/COLOR], , False, , False, False
 
Upvote 0
Try
Code:
Sub MM1()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp)) 
   .Replace "*Deal" & Chr(10) & "ID*", "#N/A", xlPart, , False, , False, False
   .SpecialCells(xlConstants, xlErrors).EntireRow.ClearContents
  End With
End Sub
 
Last edited:
Upvote 0
You have MVP in your name because it's no lie! Thank you, that worked!

I tried getting rid of the offset before and it didn't work, maybe i missed something. If you have a moment, can you tell me why a with loop is better than a for loop in this case?
 
Last edited:
Upvote 0
Glad it's sorted & thanks for the feedback.

That code doesn't actually use a loop, a With statement is simply a form of "shorthand". This is basically the same but without the With statement
Code:
Sub MM1()
   Range("A1", Cells(Rows.Count, "A").End(xlUp)).Replace "*Deal" & Chr(10) & "ID*", "#N/A", xlPart, , False, , False, False
   Range("A1", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlConstants, xlErrors).EntireRow.ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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