+ Reply to Thread
Results 1 to 11 of 11

Workbook_BeforeClose Event - passing arguments

  1. #1
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148

    Workbook_BeforeClose Event - passing arguments

    Hi,

    I'm trying to use Workbook_BeforeClose event to delete my custom menus.

    However, when I try to pass the custom menu caption to the Sub, I get a compile error (Expected Function or Variable).

    I believe this has something to do with global variables declaration or something...

    Please Login or Register  to view this content.
    Thanks in advance!
    Last edited by pmguerra; 10-09-2008 at 09:32 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437
    Where and how are you declaring the object cbcCustomMenu
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I think that the source of your problem is your using Run. Using Call should fix that
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148
    I'm declaring it in a Module in the VBA Project.

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437
    As mickerson points out your syntax is correct for the Call statement rather than Run.

  6. #6
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148
    Now it returns "byRef argument mismatch"

    After I put "byVal" in the arguments list, it returns "typemismatch"...

    I even tried declaring my variable as GLOBAL but it didn't work...

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello pmguerra,

    It would help to see the your macro code.

    Sincerely,
    Leith Ross

  8. #8
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148
    I took most of this code from an internet site on building custom menus.

    Please Login or Register  to view this content.
    Thanks for the help!

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    That code declares the variable nomeMenu three times.

    1)Inside the sub AddMenus, remove the line
    Please Login or Register  to view this content.
    2)Change the name of the argument in DeleteMenu, e.g.
    Please Login or Register  to view this content.
    3)The typemismatch is coming from the module wide declaration (before AddMenus) of nomeMenu. In ThisWorkbook, nomeMenu is not declared and therefore assumed to be a Variant rather than a String. Hence the type mismatch.

    To fix that, set your VBEditor Preferences to Require Variable Declaration. This will put Option Explicit at the start of all your modules and prevent future problems.

    For this specific situation, changing the first line in the posted code to
    Please Login or Register  to view this content.
    will give nomeMenu a global scope and set things right.
    Last edited by mikerickson; 10-08-2008 at 07:39 PM.

  10. #10
    Forum Contributor
    Join Date
    10-04-2004
    MS-Off Ver
    2013
    Posts
    148
    OK! It finally works!

    Here's the final code:

    Please Login or Register  to view this content.
    Another question

    To fix that, set your VBEditor Preferences to Require Variable Declaration. This will put Option Explicit at the start of all your modules and prevent future problems.
    How do I do this? I had to mannualy insert Option Explicit in every module...

    Thanks a lot! You've been a great help!

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    In my Excel2004 VBEditor, I go to the Excel menu item, choose Preferences from the dropdown list and click the appropriate checkbox on the Editor tab.

    In other versions of Excel, the location of that dialog box may be different. Starting a thread with a title "Automatic Option Explicit in Excel xxx" would get a quick responce tailored to your version.

+ 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. combobox.exit event
    By fozze in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-03-2008, 07:27 AM
  2. Run-Time Error 91 When Dynamically Creating Controls and Event Handlers
    By chucklod in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-21-2008, 05:47 PM
  3. Arguments for Double Click event
    By dpenny in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2007, 06:49 PM
  4. Flagging event due dates
    By muskt in forum Excel General
    Replies: 7
    Last Post: 11-19-2006, 05:44 PM
  5. Passing arguments when opening Excel
    By david.fredrikss in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-13-2006, 08:56 AM

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