Run time error 91 - explanation requred

auntie bella

Board Regular
Joined
Apr 3, 2009
Messages
50
I am getting a Run time error 91 on the code below - (Object variable or With Block variable not set).

Unfortunately as a novice at VBA I haven't a clue what this is about :(.
However the code does do what I want it to:confused:.
Can someone explain for me?

==========================================

Sub CommentAddOrEdit()
Dim rFind As Range

For Each c In Range("C1:D12")
If Not c.Comment Is Nothing Then c.Comment.Delete
Set rFind = Worksheets("Sheet2").Rows(1).Find(What:=c.Value, LookAt:=xlWhole)
If Not rFind.Comment Is Nothing Then
c.AddComment.Text rFind.Comment.Text
End If
Next c
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
That implies that the value you searched for wasn't found. You need to test for that:
Code:
Sub CommentAddOrEdit()
Dim rFind As Range

For Each c In Range("C1:D12")
If Not c.Comment Is Nothing Then c.Comment.Delete
Set rFind = Worksheets("Sheet2").Rows(1).Find(What:=c.Value, LookAt:=xlWhole)
If not rFind is Nothing then
   If Not rFind.Comment Is Nothing Then
       c.AddComment.Text rFind.Comment.Text
   End If
Else
   msgbox c.Value " was not found"
End If
Next c
End Sub
 
Last edited:
Upvote 0
And what if c.Value isn't found? Wouldn't rFind be nothing? So maybe you should test for that?

Edit:Rory has coded it ... although he says rFound instead of rFind.
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] CommentAddOrEdit()

    [COLOR="Blue"]Dim[/COLOR] rFind [COLOR="Blue"]As[/COLOR] Range, c [COLOR="Blue"]As[/COLOR] Range

    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] c [COLOR="Blue"]In[/COLOR] Range("C1:D12")
    
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] c.Comment [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR] c.Comment.Delete
    
        [COLOR="Blue"]Set[/COLOR] rFind = Worksheets("Sheet2").Rows(1).Find(What:=c.Value, LookAt:=xlWhole)
    
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] rFind [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR]
    
            [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] rFind.Comment [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR]
                c.AddComment.Text rFind.Comment.Text
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]Next[/COLOR] c
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,188
Members
452,893
Latest member
denay

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