Enter key trigger to command button on worksheet


New Member
Aug 8, 2008
Hi guys,

I'm very new to this and to visual basics. I have very limited experience in programming. I need some help. I created a search marco. It has a text box for the user to type in what they are looking for from a crazy long list located on another sheet and a command button to begin the search. Everything works fines except I cannot press the Enter key to get it to work. Is there any way around this? I only added the code to the command box.

Here is the code I made after many many many months:

Private Sub CommandButton1_Click()
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddr As String
Dim sht As Worksheet
Dim Vu
Dim intS As Integer
Dim rngC As Range
Dim strToFind As String, FirstAddress As String
Dim wSht As Worksheet
Dim myshape As Shape
Sheets("Search Results").Select

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Interior.ColorIndex = 2
For Each myshape In ActiveSheet.Shapes

If myshape.Type = 17 Then myshape.Delete

Next myshape


On Error Resume Next
Vu = SearchBox.Value
Application.ScreenUpdating = False

intS = 13
'This step assumes that you have a worksheet named
'Search Results.
Set sht = Worksheets("Search Results")
strToFind = Vu
'Change this range to suit your own needs.
With Sheets("ibccodes").Range("A1:F8000")
Set rngC = .Find(What:=strToFind, LookAt:=xlPart)
If Not rngC Is Nothing Then
FirstAddress = rngC.Address
rngC.EntireRow.Copy sht.Cells(intS, 1)
intS = intS + 2
Set rngC = .FindNext(rngC)
Loop While Not rngC Is Nothing And rngC.Address <> FirstAddress
End If
End With
If Vu = "" Then
MsgBox "No Value Entered"
Exit Sub
End If

Sheets("Search Results").Select
End Sub

Any help is appreciated.

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I think you're saying you want to be able to push enter in the textbox and have it run your search, if so, then do this:

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = vbKeyReturn Then
    End If

End Sub
Upvote 0
Thanks for the reply Chris, but unfortunatley it does not work. I remember reading somewhere that I had to put the number 13 which represents the Enter key. is that correct?
Upvote 0
You're not giving much information to know what is wrong, it works but keeps crashing doesn't tell much.

All my code does is launch your command button any time you push enter, is that crashing?if not, then it is your original code, but you said that was working ok...
Upvote 0
What i meant by crashing is that excel kept shutting down and it gives me a message wanting to send an error report. However, everything is all good now. I did some more research and apparently someone had a similar problem as mine. instead of KeyDown he entered KeyUp. Works wonderfully. Thanks for you help. Couldn't have gotten this far without ya
Upvote 0

Forum statistics

Latest member

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