+ Reply to Thread
Results 1 to 6 of 6

OnKey Error

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2023
    Location
    USA
    MS-Off Ver
    365
    Posts
    3

    OnKey Error

    I have an Excel 365 spread sheet where the user is not allowed to save with incomplete data. This works fine. But I need a way to override that for my own use and save anyway. When I use the defined key combination CTRL-ALT-PGDN I get an error 'argument not optional'. What is wrong with this code?

    Application.OnKey "%^{PgDn}", "Secretsave"

    Public Sub Secretsave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    MsgBox "In secret save"
    ThisWorkbook.Save SaveChanges = True
    End Sub

    I want it to save the workbook. Thanks. Any help is appreciated.

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: OnKey Error

    I understand that you will not need to SaveAs so use this code instead:
    Private Sub SecretSave()
        MsgBox "In secret save"
        ThisWorkbook.Save
    End Sub

  3. #3
    Registered User
    Join Date
    06-01-2023
    Location
    USA
    MS-Off Ver
    365
    Posts
    3

    Re: OnKey Error

    I wan to save and close both. But my key combo doesn't even get me to the sub, i.e., I never see the "In SecretSave" message. Should the Application.OnKey "%^{PgDn}", "Secretsave" be in a Workbook_Open subroutine?

  4. #4
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: OnKey Error

    Oh, I thought you were already clear about this part.
    The activation of the OnKey sequence goes in the ThisWorkBook module:
    Private Sub Workbook_Open()
        Application.OnKey "^%{PGDN}", "SecretSave"
    End Sub
    while the "SecretSave" macro goes in a standard module.

    Perhaps it's better to add this other code to the ThisWorkBook module to disable the OnKey sequence when finished:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Application.OnKey "^%{PGDN}", ""
    End Sub
    Last edited by rollis13; 06-05-2023 at 05:32 PM.

  5. #5
    Registered User
    Join Date
    06-01-2023
    Location
    USA
    MS-Off Ver
    365
    Posts
    3

    Re: OnKey Error

    Whether I use Workbook_Open or BeforeClose, I get "Compile Error. Sub or Function not defined".

  6. #6
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: OnKey Error

    No need to manually use those two macros, they are event macros and trigger automatically, the first on opening and the second on closing the file.
    Sorry but can't replicate your issue. The attached file is a working file.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Application.onkey error 1004 (even with very simple code)
    By MJrush in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-29-2021, 05:30 PM
  2. something about onkey
    By mrkhchan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2015, 11:15 AM
  3. onkey action error
    By Talorthain in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2013, 09:52 AM
  4. ONKey
    By insanity66 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2010, 03:46 PM
  5. Onkey gone haywire!!!
    By timatstockers in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2008, 01:28 AM
  6. [SOLVED] OnKey
    By Hernan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2006, 12:20 PM
  7. [SOLVED] Help with using OnKey
    By keithb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-19-2005, 07:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1