VBA code to press ENTER in a macro

chef

Well-known Member
Joined
Jul 10, 2002
Messages
610
Office Version
  1. 365
  2. 2016
Hi
I have this code that works fine and I type in a forename or Surname in Cells D8/E8
I have then attached this macro to a shape to search to display results in column C which works fine.
One thing I would like to try and do instead is to type in details into Cells D8 or E8 and then be able to simulate pressing enter with carriage return and then displaying results automatically in Column C instead of user having to separately click on search button.
Can this be done?

Many thanks in advance
Ian

Sub Search()
ActiveSheet.Unprotect Password:=PWD
Application.CutCopyMode = False
Range("d8:e8").Select
Selection.Interior.Color = RGB(244, 176, 132)
Range("data").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"C7:E8"), CopyToRange:=Range("C12"), Unique:=False
Range("e8").Select
ActiveSheet.Protect Password:=PWD
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi
I have this code that works fine and I type in a forename or Surname in Cells D8/E8
I have then attached this macro to a shape to search to display results in column C which works fine.
One thing I would like to try and do instead is to type in details into Cells D8 or E8 and then be able to simulate pressing enter with carriage return and then displaying results automatically in Column C instead of user having to separately click on search button.
Can this be done?

Many thanks in advance
Ian

Sub Search()
ActiveSheet.Unprotect Password:=PWD
Application.CutCopyMode = False
Range("d8:e8").Select
Selection.Interior.Color = RGB(244, 176, 132)
Range("data").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"C7:E8"), CopyToRange:=Range("C12"), Unique:=False
Range("e8").Select


You can use SendKeys.

SendKeys ~, True

The ~ is shorthand for the Enter key, and can also be typed as SendKeys {Enter}, True. True means that the programs waits for the key press to occur before carrying onwards. In more elaborate forays with this statement you'd like to also use Application.Wait to prevent premature key presses. It is also generally considered to be a means of last resort since it kind of goes in blind.
 
Last edited:
Upvote 0
Many thanks and I will try this in the macro.

Appreciate your quick response
Regards
Ian
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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