Run time error 91 - Object variable or with block variable not set

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have this code that error's when I run it.

VBA Code:
Dim RngC As Range
Set RngC = Range("C" & Rows.Count).End(xlUp)
[COLOR=rgb(226, 80, 65)]Columns("C").Find("CARTONS", After:=RngC, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Select[/COLOR]
UserForm1.CARTONSSSCC1.Text = RngC.Offset(-1, 2).Value
UserForm1.CARTONSSSCC2.Text = UserForm1.CARTONSSSCC1.Text

The red line is the one that is highlighted when it error's.
I think it is because the sheet it works on is blank at the moment, so is there something I could add in to prevent this error for happening again?

Thanks
Dan
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this update to your code & see if helps

VBA Code:
Dim RngC As Range
    
    Set RngC = Columns("C").Find("CARTONS", SearchOrder:=xlByRows, _
                                            SearchDirection:=xlPrevious, _
                                            MatchCase:=False, _
                                            SearchFormat:=False)
    If Not RngC Is Nothing Then
        UserForm1.CARTONSSSCC1.Text = RngC.Offset(-1, 2).Value
        UserForm1.CARTONSSSCC2.Text = UserForm1.CARTONSSSCC1.Text
    Else
    'not found
    MsgBox "Not Found"
    End If

Dave
 
Upvote 0
The red line is the one that is highlighted when it error's.
FYI, if you want to apply your own formatting to vba code in your post, use the 'RICH' tags, not the VBA tags
1604832329964.png
 
Upvote 0
Hi Dave,
Thank you for your code, it works perfectly.

Hi Peter,
Thank you for letting me know, I'll remember that for next time.

Thanks

Dan
 
Upvote 0

Forum statistics

Threads
1,214,517
Messages
6,119,984
Members
448,935
Latest member
ijat

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