Consulting

Results 1 to 5 of 5

Thread: Solved: vba - detect "+" and "-" keys

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    87
    Location

    Solved: vba - detect "+" and "-" keys

    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
    Regards
    Osvaldo

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    [VBA]
    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

    [/VBA]


    Paul

  3. #3
    VBAX Regular
    Joined
    Aug 2011
    Posts
    87
    Location
    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.

    [vba]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[/vba]
    Regards
    Osvaldo

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    I'd use the Selection, and just increment the numbers

    [VBA]
    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
    [/VBA]


    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

  5. #5
    VBAX Regular
    Joined
    Aug 2011
    Posts
    87
    Location
    Ok, Paul.
    I'll walk this way (Alt-UpArrow and Alt-DownArrow).
    Thanks, nice day.
    Regards
    Osvaldo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •