Detect Mouse Click and Keyboard key

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
Assuming I have two codes, how can I wrap my code to detect if Enter key was pressed or a mouse click? So, if it is enter key it should run one code, if mouse click it should run another code
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
So, if it is enter key it should run one code, if mouse click it should run another code

where are they clicking (on a sheet? on a userform? a command button?), and where is the activecell upon pressing enter?
Do you want these codes to run every single time someone clicks or presses enter?
please also include sheet names where applicable
 
Upvote 0
but as a general answer you can use "application.onkey" in a worksheet sub where you activate the sheet and press enter anywhere on the sheet and then when you leave the sheet these codes deactivate so that you have normal functionality of enter.

so lets say you copy this into sheet1's VBA module
Code:
<code>Private Sub Worksheet_Activate()
    Application.OnKey "{ENTER}", "ACode"
End Sub

</code><code><code>Private Sub Worksheet_Deactivate()
    </code></code><code><code><code>Application.OnKey "{ENTER}", ""</code>
End Sub</code></code>


That will use the numeric enter key to execute a procedure
and below is the regular enter key to execute a procedure

Code:
<code>Private Sub Worksheet_Activate()
    Application.OnKey "~", "ACode"
End Sub

</code><code><code>Private Sub Worksheet_Deactivate()
    </code></code><code><code><code>Application.OnKey "~", ""</code>
End Sub</code></code>

clicking is a different sheet specific code as well
but i would like more details before i really get into it.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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