Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Excel 2007 VBA Programmer's Reference ISBN: 978-0-470-04643-2
This is the forum to discuss the Wrox book Excel 2007 VBA Programmer's Reference by John Green, Stephen Bullen, Rob Bovey, Michael Alexander; ISBN: 9780470046432
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Excel 2007 VBA Programmer's Reference ISBN: 978-0-470-04643-2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 14th, 2010, 05:53 PM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Macro code crashes Excel 2007

Hi, I wrote this code to give selective access to tabs based on passwords inputed into the messagebox. For some reason, after I inserted the application.screenupdating the code crashes Excel everytime I close the file or try ot save/not save the file. In short, the on_close sub is going kaput...
This is the entire code...can you please help? (btw, opening the workbook is not a problem)

Code:
Private Sub Workbook_Open()
    Worksheets("Cover Page").Visible = True
    Worksheets("Control Sheet").Visible = False
    Worksheets("Channel Summary").Visible = False
    Worksheets("Club Drug").Visible = False
    Worksheets("Grocery").Visible = False
    Worksheets("Mass-Spec & Wholesale").Visible = False
    Worksheets("Mass & Specialty").Visible = False
    Worksheets("Wholesale").Visible = False
    Worksheets("Quebec").Visible = False
    Worksheets("Input Data").Visible = False
    Worksheets("2008 Target").Visible = False
    Call GetInput
End Sub
Sub GetInput()
    Dim Password As String
    Worksheets("Cover Page").Visible = True
    Worksheets("Control Sheet").Visible = False
    Worksheets("Channel Summary").Visible = False
    Worksheets("Club Drug").Visible = False
    Worksheets("Grocery").Visible = False
    Worksheets("Mass-Spec & Wholesale").Visible = False
    Worksheets("Mass & Specialty").Visible = False
    Worksheets("Wholesale").Visible = False
    Worksheets("Quebec").Visible = False
    Worksheets("Input Data").Visible = False
    Worksheets("2008 Target").Visible = False
    Application.ScreenUpdating = True
    Password = Application.InputBox(prompt:="Enter your Channel password", Type:=2)
    If Password = "xyz" Then
        Application.ScreenUpdating = False
        Worksheets("Mass & Specialty").Visible = True
        Worksheets("Cover Page").Visible = True
        Worksheets("Mass & Specialty").Activate
    ElseIf Password = "abc" Then
        Application.ScreenUpdating = False
        Worksheets("Grocery").Visible = True
        Worksheets("Cover Page").Visible = True
        Worksheets("Grocery").Activate
    ElseIf Password = "def" Then
        Application.ScreenUpdating = False
        Worksheets("Wholesale").Visible = True
        Worksheets("Cover Page").Visible = True
        Worksheets("Wholesale").Activate
    ElseIf Password = "ghi" Then
        Application.ScreenUpdating = False
        Worksheets("Club Drug").Visible = True
        Worksheets("Cover Page").Visible = True
        Worksheets("Club Drug").Activate
    ElseIf Password = "jkl" Then
        Application.ScreenUpdating = False
        Worksheets("Quebec").Visible = True
        Worksheets("Cover Page").Visible = True
        Worksheets("Quebec").Activate
    ElseIf Password = "mno" Then
        Application.ScreenUpdating = False
        Worksheets("Cover Page").Visible = True
        Worksheets("Mass & Specialty").Visible = True
        Worksheets("Wholesale").Visible = True
        Worksheets("Mass-Spec & Wholesale").Visible = True
        Worksheets("Mass-Spec & Wholesale").Activate
    ElseIf Password = "pqr" Then
        Application.ScreenUpdating = False
        Worksheets("Mass & Specialty").Visible = True
        Worksheets("Grocery").Visible = True
        Worksheets("Club Drug").Visible = True
        Worksheets("Wholesale").Visible = True
        Worksheets("Quebec").Visible = True
        Worksheets("Channel Summary").Visible = True
        Worksheets("Input Data").Visible = True
        Worksheets("Mass-Spec & Wholesale").Visible = True
        Worksheets("2008 Target").Visible = True
        Worksheets("Cover Page").Visible = True
        Worksheets("Control Sheet").Visible = True
    ElseIf Password = "stu" Then
        Application.ScreenUpdating = False
        Worksheets("Mass & Specialty").Visible = True
        Worksheets("Grocery").Visible = True
        Worksheets("Club Drug").Visible = True
        Worksheets("Wholesale").Visible = True
        Worksheets("Quebec").Visible = True
        Worksheets("Channel Summary").Visible = True
        Worksheets("Input Data").Visible = False
        Worksheets("Mass-Spec & Wholesale").Visible = False
        Worksheets("Cover Page").Visible = False
        Worksheets("2008 Target").Visible = False
        Worksheets("Control Sheet").Visible = False
        Worksheets("Channel Summary").Activate
    Else
        Application.ScreenUpdating = False
        MsgBox ("Incorrect Password")
        Worksheets("Channel Summary").Visible = False
        Worksheets("Club Drug").Visible = False
        Worksheets("Grocery").Visible = False
        Worksheets("Mass-Spec & Wholesale").Visible = False
        Worksheets("Mass & Specialty").Visible = False
        Worksheets("Wholesale").Visible = False
        Worksheets("Quebec").Visible = False
        Worksheets("Input Data").Visible = False
        Worksheets("2008 Target").Visible = False
        Worksheets("Control Sheet").Visible = False
        Worksheets("Cover Page").Visible = True
    End If
            
    End Sub
    
 Private Sub Workbook_beforeClose(cancel As Boolean)
    If ActiveWorkbook.Saved = True Then
        ActiveWorkbook.Close
    Exit Sub
    Else
        Dim Msg As String
        Msg = "Do you want to save "
        Msg = Msg & ActiveWorkbook.Name & " before closing" & "?"
        Ans = MsgBox(Msg, vbQuestion + vbYesNoCancel)
        Select Case Ans
            Case vbYes
                Worksheets("Cover Page").Visible = True
                Worksheets("Channel Summary").Visible = False
                Worksheets("Club Drug").Visible = False
                Worksheets("Grocery").Visible = False
                Worksheets("Mass-Spec & Wholesale").Visible = False
                Worksheets("Mass & Specialty").Visible = False
                Worksheets("Wholesale").Visible = False
                Worksheets("Quebec").Visible = False
                Worksheets("Input Data").Visible = False
                Worksheets("2008 Target").Visible = False
                Worksheets("Control Sheet").Visible = False
                ActiveWorkbook.Save
                ActiveWorkbook.Close
            Case vbNo
                Worksheets("Cover Page").Visible = True
                Worksheets("Channel Summary").Visible = False
                Worksheets("Club Drug").Visible = False
                Worksheets("Grocery").Visible = False
                Worksheets("Mass-Spec & Wholesale").Visible = False
                Worksheets("Mass & Specialty").Visible = False
                Worksheets("Wholesale").Visible = False
                Worksheets("Quebec").Visible = False
                Worksheets("Input Data").Visible = False
                Worksheets("2008 Target").Visible = False
                Worksheets("Control Sheet").Visible = False
                ActiveWorkbook.Saved = True
                ActiveWorkbook.Close
                Workbooks("Personal.xlsb").Saved = True
                Workbooks("Personal.xlsb").Close
                On Error GoTo 0
            Case vbCancel
                cancel = True
         End Select
    End If
 End Sub
Reply With Quote
  #2 (permalink)  
Old March 14th, 2010, 06:56 PM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I figured it out...I was trying to close another workbook (personal.xlsb)from the macro (after closing the active workbook containing the macro itself)...the problem was not with the screenupdating property.
Thanks

any other insights/causes, please let me know. This is the modified code for the Before_Close sub I wrote which doesn't crash excel now

Code:
 Private Sub Workbook_beforeClose(cancel As Boolean)
    Application.ScreenUpdating = True
    If ActiveWorkbook.Saved = True Then
        ActiveWorkbook.Close
    'Exit Sub
    Else
        Dim Msg As String
        Msg = "Do you want to save "
        Msg = Msg & ActiveWorkbook.Name & " before closing" & "?"
        Ans = MsgBox(Msg, vbQuestion + vbYesNoCancel)
        Select Case Ans
            Case vbYes
                Worksheets("Cover Page").Visible = True
                Worksheets("Channel Summary").Visible = False
                Worksheets("Club Drug").Visible = False
                Worksheets("Grocery").Visible = False
                Worksheets("Mass-Spec & Wholesale").Visible = False
                Worksheets("Mass & Specialty").Visible = False
                Worksheets("Wholesale").Visible = False
                Worksheets("Quebec").Visible = False
                Worksheets("Input Data").Visible = False
                Worksheets("2008 Target").Visible = False
                Worksheets("Control Sheet").Visible = False
                ActiveWorkbook.SaveAs
                ActiveWorkbook.Close
            Case vbNo
                Worksheets("Cover Page").Visible = True
                Worksheets("Channel Summary").Visible = False
                Worksheets("Club Drug").Visible = False
                Worksheets("Grocery").Visible = False
                Worksheets("Mass-Spec & Wholesale").Visible = False
                Worksheets("Mass & Specialty").Visible = False
                Worksheets("Wholesale").Visible = False
                Worksheets("Quebec").Visible = False
                Worksheets("Input Data").Visible = False
                Worksheets("2008 Target").Visible = False
                Worksheets("Control Sheet").Visible = False
                ActiveWorkbook.Saved = True
                Workbooks("Personal.xlsb").Saved = True
                Workbooks("Personal.xlsb").Close
                ActiveWorkbook.Close
                    On Error GoTo 0
            Case vbCancel
                cancel = True
         End Select
    End If
 End Sub
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to specify relative path to Access 2007 in ADO connection string? [Excel 2007] sektor Excel VBA 4 September 4th, 2009 12:59 AM
Excel crashes sunny76 Excel VBA 0 July 2nd, 2008 09:25 PM
ecel 2007 macro nankeen Excel VBA 0 December 2nd, 2007 11:40 PM
Excel crashes when same report is run again carrie09 VB Databases Basics 1 September 20th, 2007 03:21 PM
Access 2007 - save macro as event proc jesleon Access VBA 0 February 3rd, 2007 09:10 PM



All times are GMT -4. The time now is 07:02 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.