how do I make a macro run on any key press

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,362
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
How do I get vba code to run if any key if pressed?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Can be done Dave.......but that would / could open a pandoras box of problems....what if you didn't want the macro to run, just wanted to type something into a cell ??
Might be easier to look at a Selection_change event
 
Last edited:
Upvote 0
I tried to enter this in the worksheet change event but it didn't work.

Code:
Application.OnKey "{DELETE}", ThisWorkbook.Worksheets("CSS_quote_sheet").Range("C11").Formula = "=IF(A11="""","""",IF(COUNTIF(Sheet2!$G$87:$DO$97,A11),""Public_holiday"",IF(WEEKDAY(A11)=1,""Sun"",IF(WEEKDAY(A11)=7,""Sat"",""Business_day_rate""))))"
 
Upvote 0
Post ALL the code and did you put in the sheet module ??
 
Upvote 0
I can't post all of the code for privacy issues.

The start of the procedure looks like this:

The only bit I added to the on change event was the last line
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.OnKey "{DELETE}", ThisWorkbook.Worksheets("CSS_quote_sheet").Range("C11").Formula = "=IF(A11="""","""",IF(COUNTIF(Sheet2!$G$87:$DO$97,A11),""Public_holiday"",IF(WEEKDAY(A11)=1,""Sun"",IF(WEEKDAY(A11)=7,""Sat"",""Business_day_rate""))))"
 
Last edited:
Upvote 0
Ok, so why do you need a delete ON Key.
Why not simply use the cell location, so when the cell is cleared, this code runs
Change the text in red to your cell

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("[color=red]YOUR CELL[/color]")) Is Nothing Then Exit Sub
Worksheets("CSS_quote_sheet").Range("C11").Formula = "=IF(A11="""","""",IF(COUNTIF(Sheet2!$G$87:$DO$97,A11),""Public_holiday"",IF(WEEKDAY(A11)=1,""Sun"",IF(WEEKDAY(A11)=7,""Sat"",""Business_day_rate""))))"
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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