Enter Key = macro execution

felipepaz

New Member
Joined
Jan 8, 2016
Messages
2
Hello guys, I'm new here!

I've created an event key to run another macro. My macro does a search on the worksheet and returns values like monthly billing, store code, city and others. I write the criterion I want on A5:G5 and it returns on A6:G?? ... my problem is when I type text the macro does not work, only when I click on the search icon it does but, when I write numbers on any cell of the search the macro works perfectly and I don't need to click on search icon.

What can it be?

Code:
Public Sub Pesquisa()

     Sheets("PESQUISA").Select
    Sheets("basepes").Visible = True
    Sheets("PESQUISA").Select
    Sheets("basepes").Range("A1:L5900").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Range("PESQUISA!Criteria"), CopyToRange:=Range( _
        "PESQUISA!Extract"), Unique:=False
    Sheets("basepes").Select
    ActiveWindow.SelectedSheets.Visible = False
    Range("A5:L5").Select
    Selection.ClearContents
    Sheets("PESQUISA").Select
    Range("A5").Select
    Application.OnKey "{ENTER}", "Pesquisa"
    
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi ,
Welcome to the board.
If you want to execute this code when you press Enter ( {ENTER} is enter key from the numpad ) a better way would be to write this code in workbook activate event.
Code:
Private Sub Workbook_Activate()
  Application.OnKey "{ENTER}", "[COLOR=#333333]Pesquisa[/COLOR]"
End Sub

If you want the search results to be populated as soon as you change values in cells A5:G5 you need to write the code like this in worksheet change event.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A5:G5")) Is Nothing Then
        Sheets("PESQUISA").Select
        Sheets("basepes").Visible = True
        Sheets("PESQUISA").Select
        Sheets("basepes").Range("A1:L5900").AdvancedFilter Action:=xlFilterCopy, _
                                                           CriteriaRange:=Range("PESQUISA!Criteria"), CopyToRange:=Range( _
                                                                                                                   "PESQUISA!Extract"), Unique:=False
        Sheets("basepes").Select
        ActiveWindow.SelectedSheets.Visible = False
        Range("A5:L5").Select
        Selection.ClearContents
        Sheets("PESQUISA").Select
        Range("A5").Select
        Application.OnKey "{ENTER}", "Pesquisa"
    End If
End Sub

Hope it helps.
 
Upvote 0
Hi ,
Welcome to the board.
If you want to execute this code when you press Enter ( {ENTER} is enter key from the numpad ) a better way would be to write this code in workbook activate event.
Code:
Private Sub Workbook_Activate()
  Application.OnKey "{ENTER}", "[COLOR=#333333]Pesquisa[/COLOR]"
End Sub

If you want the search results to be populated as soon as you change values in cells A5:G5 you need to write the code like this in worksheet change event.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A5:G5")) Is Nothing Then
        Sheets("PESQUISA").Select
        Sheets("basepes").Visible = True
        Sheets("PESQUISA").Select
        Sheets("basepes").Range("A1:L5900").AdvancedFilter Action:=xlFilterCopy, _
                                                           CriteriaRange:=Range("PESQUISA!Criteria"), CopyToRange:=Range( _
                                                                                                                   "PESQUISA!Extract"), Unique:=False
        Sheets("basepes").Select
        ActiveWindow.SelectedSheets.Visible = False
        Range("A5:L5").Select
        Selection.ClearContents
        Sheets("PESQUISA").Select
        Range("A5").Select
        Application.OnKey "{ENTER}", "Pesquisa"
    End If
End Sub

Hope it helps.


You won't believe but I've solved this issue just adding two more lines on my code.

Code:
 Public Sub Pesquisa()'
' Pesquisa Macro
'


     Sheets("PESQUISA").Select
    Sheets("basepes").Visible = True
    Sheets("PESQUISA").Select
    Sheets("basepes").Range("A1:L5900").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Range("PESQUISA!Criteria"), CopyToRange:=Range( _
        "PESQUISA!Extract"), Unique:=False
    Sheets("basepes").Select
    ActiveWindow.SelectedSheets.Visible = False
    Range("A5:L5").Select
    Selection.ClearContents
    Sheets("PESQUISA").Select
    Range("A5").Select
    Application.OnKey "~", "Pesquisa"
    Application.OnKey "{ENTER}", "Pesquisa"

Now, the users can run the macro pressing enter or enter numpad and it works perfectly. Thank you very much for the attention!!
 
Upvote 0

Forum statistics

Threads
1,214,886
Messages
6,122,093
Members
449,064
Latest member
Danger_SF

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