Runtime error #91, Object variable or with block variable not set

Vivek Roshan

New Member
Joined
Feb 15, 2018
Messages
24
hi Mr. Fluff

I have added search command button to userform in excel VBA and wanted to search the data through the dates displayed in combobox. The idea is to search date in worksheet called ICC_Data and if it finds, then it will display the data in TextBox1, TextBox2 and TextBox3 of userform. When I am selecting the date in combobox and pressing search command button. First, it coverts the date into 5 digit value and then shows an error as:

Runtime error 91, object variable or with block variable is not set.


My code is given below and in my Workbook Sheet2 is named as ICC_Data.
-------------------------------------------------------------------------------------
Private Sub CmdSearch_Click()

Dim Findvalue As Range


Set Findvalue = Sheet2.Range("A:A").Find(what:=Me.ComboBox1.Value, LookIn:=xlFormulas, lookat:=xlWhole)

Me.TextBox1.Value = Findvalue.Value
Me.TextBox2.Value = Findvalue.Offset(0, 1).Value
Me.TextBox3.Value = Findvalue.Offset(0, 2).Value


End sub
---------------------------------------------------------------------------------------------------------

Can you please help me to resolve this problem.

Regards,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello,
you will have an error if nothing is found use,,
Code:
    Dim Findvalue As Range
    Set Findvalue = Range("A:A").Find(what:=Me.ComboBox1.Value, LookIn:=xlFormulas, lookat:=xlWhole)
    If Not Findvalue Is Nothing Then
        Me.TextBox1.Value = Findvalue.Value
        Me.TextBox2.Value = Findvalue.Offset(0, 1).Value
        Me.TextBox3.Value = Findvalue.Offset(0, 2).Value
    End If
 
Upvote 0
Hi Vevek Roshan

You'll have to show exactly on what line the error is occurring - I'm not getting the same error as you (rather errors particular to the fact I don't have UserForms in the workbook in which I'm testing your code).

Step through your code using F8 (or otherwise run it) and let us know which line it stops at.

Cheers

pvr928
 
Upvote 0
Hi pvr,

I am getting error on the highlighted code:

-----------------------------------
Private Sub CmdSearch_Click()

Dim Findvalue As Range

Set Findvalue = Sheet2.Range("A:A").Find(what:=Me.ComboBox1.Value, LookIn:=xlFormulas, lookat:=xlWhole)

Me.TextBox1.Value = Findvalue.Value

Me.TextBox2.Value = Findvalue.Offset(0, 1).Value
Me.TextBox3.Value = Findvalue.Offset(0, 2).Value

End sub


 
Upvote 0
Hi Pike,

I have tried your code. It returns only 5 digit figure in combobox itself. Data is not transferred to respective text boxes in userform. I do not see in your code the worksheet reference. Actually, I am having a userform in Sheet1 which has combobox with search button and with text boxes. Combobox gets listed automatically with dates that I enter through TextBox1. Now I want to search with dates in the combobox. If I click on the date, it will show the data in TextBox1, TextBox2 and TextBox3. This is what exactly I want. For you to help me, I want to mention that my data in Sheet2 named as ICC_Data.

Please see if you can help me. Will be highly appreciated.
 
Upvote 0
when using the Find Method to Find Dates things can become somewhat tricky
Code:
Option Explicit

Private Sub CommandButton1_Click()
    Dim Findvalue As Range
    Dim strDate As String

    strDate = Format(CDate(Me.ComboBox1.Value), "Short Date")
    Set Findvalue = Sheet2.Range("A:A").Find(What:=CDate(strDate), LookIn:=xlFormulas, LookAt:=xlWhole)
    If Not Findvalue Is Nothing Then
        Me.TextBox1.Value = Findvalue.Value
        Me.TextBox2.Value = Findvalue.Offset(0, 1).Value
        Me.TextBox3.Value = Findvalue.Offset(0, 2).Value
    End If
End Sub
 
Last edited:
Upvote 0
Hello Pike,

Thanks a lot for your coding which worked magically. But I face a small problem. When search button is pressed with the selected date in combobox list, the date gets changed to 5 digit number. However, the display of all relevant data in respective textboxes is ok. Any suggestion on this issue will be appreciated.

Thanks once again for your valuable help.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,588
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