detecting keypress(es) in a cell

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

As per subject... is there a way to do this?

I would like to detect the Enter key code and SHIFT+Enter keys combination.

TIA

Hernan.
 
If the user is editing a cell, then for any practical purpose, macros don't
run.
 
hmmm...

how about on "exiting" the cell?


Tom Ogilvy said:
If the user is editing a cell, then for any practical purpose, macros don't
run.
 
I would like to detect the Enter key code and SHIFT+Enter keys combination.

Application.Onkey can be used to respond to keyboard events.


Regards,
Vic Eldridge
 
Good thought.

Sub SetKey()
Application.OnKey "~", "MyEnter"
Application.OnKey "+~", "MyShiftEnter"
End Sub

Sub UnSetKey()
Application.OnKey "~"
Application.OnKey "+~"
End Sub

Sub MyEnter()
MsgBox "In MyEnter"
End Sub
Sub MyShiftEnter()
MsgBox "In MyShiftEnter"
End Sub
 
Thank you Dave and Vicfor your help on this, and you Tom for the example.


Hernan.
 
I know this is an old, old thread.

What I want is that for cells in the named range Range("WeekDays") that on
typinging "+" the value is increased by 0.25 and on pressing "-" the value
decreased by 0.25.

What's the best way of achieving that in Excel 2003 ?
 
I don't think you can code the + and - keys, as they put any cell directly
into edit mode. So how about the left and right arrow keys

in the code below, the tow keys set a value to increase or decrease a cell
or cells by . this handles multiple cells

Option Explicit
Sub setkeys()
Application.OnKey "{right}", "IncreaseCellValue"
Application.OnKey "{left}", "DecreaseCellValue"
End Sub

Sub unsetkeys()
Application.OnKey "{right}"
Application.OnKey "{left}"
End Sub

Sub IncreaseCellValue()
ChangeCellValue 0.25
End Sub
Sub DecreaseCellValue()
ChangeCellValue -0.25
End Sub

Sub ChangeCellValue(amount As Double)
Dim target As Range
Dim cell As Range
Set target = Intersect(Selection, Range("range1"))
If Not target Is Nothing Then
For Each cell In target
If IsNumeric(cell.Value) Then
cell.Value = cell.Value + amount
End If
Next
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top