+ Reply to Thread
Results 1 to 3 of 3

Hitting return key, linked to a button

Hybrid View

  1. #1
    Registered User
    Join Date
    09-15-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    10

    Hitting return key, linked to a button

    I have a search setup in an excel sheet. Simply, I want to be able to hit the enter key and then it would automatically perform the function dedicated the button within the excel sheet.

    Example: I type in 'Cat' and hit enter, it will use the code for the search button I created. So instead of clicking the search button, the enter key will act as if I did.

    Do you know the code for that?

    Thank you in advance.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Hitting return key, linked to a button

    I've never done this before but see if this works.

    1. Modify your macro so that it brings in a string value. Something like this
    Sub testing (ByVal TestVal As String)
    Then set TestVal to whatever variable your macro searches for.

    2. In the spreadsheet where you are entering your values, enter the code
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TestVal As String
    
    If Target.Column <> 1 Then Exit Sub  'Assuming you are entering value in Column A
    TestVal = Target.Value
    
    Testing (TestVal)  'replace with your macro name
    
    End Sub
    Try this and see if it works for you.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    09-15-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Hitting return key, linked to a button

    I tried your code, I'm making it more complicated than it should be. Maybe you can help.

    Thanks in advance.

    
    Private Sub Search_Click()
    Dim LR As Long, Val As String, cell As Range, Rng As Range, Code As Boolean
    Dim dSh As Worksheet, v
    
    'Setup
        Sheets("SEARCH BY ITEM CODE").Activate
        'First Search range
            If Range("C2") <> "" Then
                Val = Range("C2").Value & ""
                Code = True
            'ElseIf Range("E3") <> "" Then
                'Val = Range("E3").Value
                'Code = False
            Else
                MsgBox "Please enter ONE search criteria and try again."
                Exit Sub
            End If
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Set dSh = Sheets("DATABASE")
        Range("A9:G200").ClearContents
    
    'Transfer matching data
        LR = dSh.Range("A" & Rows.Count).End(xlUp).Row
        dSh.Range("B2").AutoFilter
        
        If Code Then
            dSh.Range("B2").AutoFilter Field:=2, Criteria1:="=*" & Val & "*"
        Else
            dSh.Range("B2").AutoFilter Field:=3, Criteria1:="=*" & Val & "*"
        End If
        
        dSh.Range("B2:G" & LR).SpecialCells(xlCellTypeVisible).Copy Range("B9")
        dSh.Range("B2").AutoFilter

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1