Results 1 to 4 of 4

Thread: Application.OnKey prblm in Excel 2003

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Application.OnKey prblm in Excel 2003

    I have this small piece of vba code in ThisWorkbook object:

    Private Sub Workbook_Open()
    Application.OnKey "%{z}", "OptionListBox"
    End Sub

    Private Sub OptionListBox()
    Userform1.Show
    End Sub

    What it does is the user can press Ctrl-z at any moment to launch a form from my workbook. Easy enough.

    Everything is kosher except that, if I save the workbook (WB1) under a new name (WB2), then press ctrl-z, it loads the previous file(WB1) and runs the code from there. In other word, the call is being make from that old file.Grrr..

    Ive tried to incorporate some With ActiveWorkbook in some key places in the code, but no success whatsoever.

    I don't know how to attach this to a specific file, can't I run two instances for seperate workbooks on my screen?
    Can anyone help me plz?

    Thanx

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Application.OnKey prblm in Excel 2003

    Ok, I have dealt with something similar in Word before so its practically the same thing.

    What you need to be doing is when you do your saveas you need to copy over the ThisWorkbook class code to
    the new workbook, since the saveas doesnt copy the vba code modules/classes.

    Or you could add your code to the xlt template so its centralized and all your saveas workbooks will reference this template.

    Is this a local app or are you supporting other users?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Application.OnKey prblm in Excel 2003

    Hi there Rob!

    I know that when I save the workbook under another name, all the code is being included as well. So say I have Data2004.xls with bunch of code in it, if I save it under Data2005.xls, I will now have 2 files with the same code in it.

    Now keep in mind that I'm still fairly a beginner with coding, so perhaps I'm not grasping what you're saying.

    My problem is that the onkey event seems to be only launching from the 1st workbook. But I think I just solved it. I will post my result and the code if it works.

    But If you could give me a quick insight on how to post my code, on this board, in the vbe format like you guys do, that'd be great. You click Code or VBCode?

    Tx

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Application.OnKey prblm in Excel 2003

    For future reference here's what I've done:

    VB Code:
    1. Code in ThisWorkbook:
    2. ===========================
    3. Private Sub Workbook_Activate()
    4. TrapKey
    5. End Sub
    6.  
    7. Private Sub Workbook_BeforeClose(Cancel As Boolean)
    8. Application.OnKey "%{z}"
    9. End Sub
    10.  
    11. Code in Module:
    12. ===========================
    13. Sub TrapKey()
    14.     If ActiveWorkbook Is ThisWorkbook Then _
    15.         Application.OnKey "%{z}", "LoadMyForm"
    16. End Sub
    Last edited by D-niss; Apr 25th, 2005 at 05:10 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width