Application OnKey ctrl shift +

AshleyKitsune

New Member
Joined
Nov 30, 2021
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi all, this is my first post here, but I've been a long time learner of vba. This is the first time I've ever tried to do something like this and I haven't been able to make it work, sadly.

I wanted to create a key combination that would add 1 to a cells value if it was a date. I also intend to do a subtraction method. After reviewing online for over an hour, I figured it was time to ask for help.

The key combination desired was Ctrl + shift + the Plus key (on the number pad)

The thing is - I want to be able to use this no matter what spreadsheet I have open, so I put it inside my personal.xlsb workbook's workbook tab.. is this not possible? Or do you think my company's policy settings are preventing me? Or am I just plain doing it wrong?

Thank you, Ashley.

VBA Code:
Option Explicit
Private Sub Workbook_Activate()
    Application.OnKey "+^{+}", "AddDayToDate()"
End Sub
Private Sub Workbook_Deactivate()
    Application.OnKey "+^{+}"
End Sub

Sub AddDayToDate()
'
' AddDayToDate Macro
'

Dim myDate As Date

If IsDate(ActiveCell) Then
    myDate = ActiveCell.Value + 1
    ActiveCell.Value = myDate
    Else: MsgBox "Cell is not a date value."
End If
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Use "^+{107}" for the key code.
Thank you for that. I updated the string, however it's still not running at all. There's no error. I've even closed the program to see if I needed to "activate" the page. And just now I tried using the even "Workbook_open" instead, with no results.
 
Upvote 0
Remove the parentheses:
VBA Code:
Application.OnKey "^+{107}", "AddDayToDate"

Are you executing that line using the Workbook_open of the personal workbook ?
And where is the AddDayToDate routine ? Is it in a separate standard module ?

See if this works.

Entire code goes in the ThisWorkbook Module:
VBA Code:
Private Sub Workbook_Open()
    Application.OnKey "^+{107}", Me.CodeName & ".AddDayToDate"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnKey "^+{107}"
End Sub


Private Sub AddDayToDate()

    ' AddDayToDate Macro
    
    Dim myDate As Date
    
    If IsDate(ActiveCell) Then
        myDate = ActiveCell.Value + 1
        ActiveCell.Value = myDate
        Else: MsgBox "Cell is not a date value."
    End If

End Sub
 
Last edited:
Upvote 0
Solution
I discovered that you can use the constants vbKeyAdd and vbKeySubtract in place of the codes. With the add and subtract methods both working, this is the result:
VBA Code:
Private Sub Workbook_Open()
    Application.OnKey "^+{" & vbKeyAdd & "}", Me.CodeName & ".AddDayToDate"
    Application.OnKey "^+{" & vbKeySubtract & "}", Me.CodeName & ".SubtractDayFromDate"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnKey "^+{" & vbKeyAdd & "}"
End Sub

Private Sub AddDayToDate()
    
    Dim myDate As Date
    
    If IsDate(ActiveCell) Then
        myDate = ActiveCell.Value + 1
        ActiveCell.Value = myDate
        Else: MsgBox "Cell is not a date value."
    End If

End Sub

Sub SubtractDayFromDate()

    Dim myDate As Date
    
    If IsDate(ActiveCell) Then
        myDate = ActiveCell.Value - 1
        ActiveCell.Value = myDate
        Else: MsgBox "Cell is not a date value."
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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