inputbox get run-time error 91

reddcannon

Board Regular
Joined
Aug 11, 2011
Messages
118
I have this button with the code below it works 1 or 2 times, then I get the Run-time error 91, object variable or with block variable not set.

It will not work at all if I protect my worksheet with a password, which I must do. This spreadsheet is just for people to enter a part number and look up prices. That is it. I just need them to click the button and type the part number and it finds it.


Private Sub CommandButton2_Click()

'Opens box and ask what do they want to search
searchthis = InputBox("Type in a location keyword.", "Property Search")
'Tells where to search
Columns("a").Select
'and then search in them whatever the user entered:
Selection.Find(What:=searchthis, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext).Activate
End Sub
 

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.
Try

Code:
Private Sub CommandButton2_Click()
Dim searchthis As String, Found As Range
searchthis = InputBox("Type in a location keyword.", "Property Search")
Set Found = Columns("A").Find(What:=searchthis, LookIn:=xlFormulas, LookAt:=xlPart)
If Not Found Is Nothing Then Found.Select
End Sub
 
Upvote 0
Peter, thanks a million, but I should have said, I need it to look in Column A and C I tried changing it to a:c that did not work.
 
Upvote 0
Try

Rich (BB code):
Private Sub CommandButton2_Click()
Dim searchthis As String, Found As Range
searchthis = InputBox("Type in a location keyword.", "Property Search")
Set Found = Range("A:A,C:C").Find(What:=searchthis, LookIn:=xlFormulas, LookAt:=xlPart)
If Not Found Is Nothing Then Found.Select
End Sub
 
Upvote 0
OK Peter, it works, but as soon as I save it with the protect worksheet password and reopen, it does not lookup
 
Upvote 0
Try like this, change xxx to suit

Code:
Private Sub CommandButton2_Click()
Dim searchthis As String, Found As Range
Me.Unprotect Password:="xxx"
searchthis = InputBox("Type in a location keyword.", "Property Search")
Set Found = Range("A:A,C:C").Find(What:=searchthis, LookIn:=xlFormulas, LookAt:=xlPart)
If Not Found Is Nothing Then Found.Select
Me.Protect Password:="xxx"
End Sub
 
Upvote 0
FREAKING GENIUS, Man o Man, you have saved my day. Do you all do this for free, can a donation be sent or what. I am totally new to these forums.
 
Upvote 0
All for free (fortunately for you, not for me :)).

Send me a virtual G&T, thanks :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,245
Members
452,900
Latest member
LisaGo

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