Detect Mouse Click and Keyboard key

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
267
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
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
779
Office Version
2016
Platform
Windows
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
 

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
779
Office Version
2016
Platform
Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,438
Messages
5,486,891
Members
407,570
Latest member
cannotquitexcel

This Week's Hot Topics

Top