View Full Version : Solved: vba - detect "+" and "-" keys
omp001
03-08-2012, 12:22 PM
Hi all.
Is there a way to detect if that keys were typed even if "Enter" key was not ?
something like this:
If typed "+" then do stuff1
If typed "-" the do stuff2
Thnaks in advance
Paul_Hossler
03-08-2012, 12:59 PM
Option Explicit
Dim bDoStuff As Boolean
Sub DoStuff()
bDoStuff = True
'doesn't work for keypad plus / minus
'plus + is special for OnKey so need the braces
Call Application.OnKey("{+}", "DoStuff1")
Call Application.OnKey("-", "DoStuff2")
End Sub
Sub StopDoingStuff()
bDoStuff = False
Application.OnKey "{+}"
Application.OnKey "-"
End Sub
Sub DoStuff1()
MsgBox "Doing Stuff 1"
End Sub
Sub DoStuff2()
MsgBox "Doing Stuff 2"
End Sub
Paul
omp001
03-08-2012, 02:42 PM
Hey, Paul.
Thank you so much for responding.
I think I was not clear enough with my question.
What I'm after is about the possibility of triggering an operation, e.g., adding a unit value to the value of the selected cell, as the user presses only the plus sign, without the need to press "Enter" key after.
Currently I am using a WS_Change, piece of the code below. Which works well. But the amount of entries into the sheets is very large, so I thought that if the addition could be triggered by pressing only one key, any one of them.
Is it possible?
Thanks again for your time.
Thanks again for your time.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "+" Then
Application.EnableEvents = False
Application.Undo
Target.Value = Target.Value + 1
Application.EnableEvents = True
End If
End Sub
Paul_Hossler
03-08-2012, 08:05 PM
I'd use the Selection, and just increment the numbers
Sub DoStuff1()
Dim rCell As Range, rData As Range
If Not TypeOf Selection Is Range Then Exit Sub
Set rData = Intersect(Selection, Selection.Parent.UsedRange)
If rData Is Nothing Then Exit Sub
For Each rCell In rData.Cells
If IsNumeric(rCell.Value) Then rCell.Value = rCell.Value + 1
Next
End Sub
I do something this to bump dates +/- a day or as month or a year
However, I wouldn't use the + and the - keys, but something like Alt-UpArrow and Alt-DownArrow
Paul
omp001
03-09-2012, 04:11 AM
Ok, Paul.
I'll walk this way (Alt-UpArrow and Alt-DownArrow).
Thanks, nice day.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.