Results 1 to 11 of 11

Thread: [RESOLVED] BeforeClose

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    Resolved [RESOLVED] BeforeClose

    I have the following routines inThisWorkbook but althougg I can get the auto_open to work I can't get the BeforeClose to work.

    Anyone have any ideas?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub auto_open()
    4.  
    5. For Each ws In ThisWorkbook.Worksheets
    6. If Not ws.Name = "Error" Then
    7. ws.Visible = xlSheetVisible
    8. Else
    9. End If
    10. Next ws
    11.  
    12. Worksheets("Error").Visible = xlSheetVeryHidden
    13.  
    14. End Sub
    15.  
    16. Private Sub Workbook_BeforeClose(Cancel As Boolean)
    17.  
    18. Application.EnableEvents = False
    19.  
    20. MsgBox ("Closing")
    21.  
    22. If Not ThisWorkbook.Saved Then
    23. Else
    24. Worksheets("Error").Visible = xlSheetVisible
    25. For Each ws In ThisWorkbook.Worksheets
    26. If Not ws.Name = "Error" Then
    27. ws.Visible = xlSheetVeryHidden
    28. Else
    29. ws.Visible = xlSheetVisible
    30. End If
    31. Next ws
    32. ThisWorkbook.Save
    33. End If
    34.  
    35. End Sub


    Thanks
    Last edited by si_the_geek; Apr 5th, 2006 at 05:42 AM. Reason: corrected VBCode tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: BeforeClose

    Moved to Office Development forum.

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: BeforeClose

    Shall I go through your code and first decipher what it supposed to do before trying to figure out how it isn't working? Or can you answer those questions and make it a lot easier?

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    Re: BeforeClose

    Actually I think I have just cracked it.

    Need to restart Excel when after the code has been dropped in. Not sure why, but it seems to work.

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

    Re: BeforeClose

    Why would you need to restart Excel to make this work?

    What version of Excel are you running? You shouldnt use the Auto_Open event as its old and only for backwards compatibility.
    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

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    Re: BeforeClose

    Don't know.

    Just seemed to work......

    What should I use instead of auto_open?

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

    Re: BeforeClose

    VB Code:
    1. Private Sub Workbook_Open()
    2.     MsgBox "Open"
    3. End Sub
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    Re: BeforeClose

    In the Auto_Open (nowWorksheet_Open) I want to place a call to another routine that is in a module.

    It doesn't seem to recognise the sub name. How do I call another routine when script is behind ThisWorkbook?


    Thanks

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

    Re: BeforeClose

    The sub in the module should be declared as Friend or Public.
    VB Code:
    1. Public Sub SomeSubOfMine()
    2.     MsgBox "Meow!"
    3. End Sub
    Then call as usual from ThisWorkbook.
    VB Code:
    1. Private Sub WorkBook_Open()
    2.     SomeSubOfMine
    3. End Sub
    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

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    50

    Re: BeforeClose

    Right, I think I have it tied down now. Thanks all for the help.

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

    Re: BeforeClose

    Np, dont forget to Resolve the thread then.
    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

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