Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2010, 04: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
 
Old March 14th, 2010, 05: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





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 3rd, 2009 11:59 PM
Excel crashes sunny76 Excel VBA 0 July 2nd, 2008 08: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 02:21 PM
Access 2007 - save macro as event proc jesleon Access VBA 0 February 3rd, 2007 09:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.