Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Setup / February 2007

Tip: Looking for answers? Try searching our database.

Order for VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nobbyknownowt - 19 Feb 2007 12:57 GMT
Is there any specific order that VBA should be put? I have a workbook
which I want users not to be able to save. I have code to hide the
sheets and leave a prompt sheet to make users enable macros and i also
have code to not allow saves. They both work individually but when i
put them together however the hide sheet code fails.

Have put the code below
Could someone set me on the correct road please? Thanks
Nobby

Option Explicit

Private Sub Workbook_Open()

With Application
'disable the ESC key
.EnableCancelKey = xlDisabled
.ScreenUpdating = False

Call UnhideSheets

.ScreenUpdating = True
're-enable ESC key
.EnableCancelKey = xlInterrupt
End With

End Sub
'
Private Sub UnhideSheets()
'
Dim Sheet As Object
'
For Each Sheet In Sheets
If Not Sheet.Name = "Prompt" Then
Sheet.Visible = xlSheetVisible
End If
Next
'
Sheets("Prompt").Visible = xlSheetVeryHidden
'
Set Sheet = Nothing
ActiveWorkbook.Saved = True

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
With Application
.EnableCancelKey = xlDisabled
.ScreenUpdating = False

Call HideSheets

.ScreenUpdating = True
.EnableCancelKey = xlInterrupt
End With
End Sub

Private Sub HideSheets()
'
Dim Sheet As Object                             '< Includes
worksheets and chartsheets
'
With Sheets("Prompt")
'
'the hiding of the sheets constitutes a change that
generates
'an automatic "Save?" prompt, so IF the book has already
'been saved prior to this point, the next line and the
lines
'relating to .[A100] below bypass the "Save?" dialog...
If ThisWorkbook.Saved = True Then .[A100] = "Saved"
'
.Visible = xlSheetVisible
'
For Each Sheet In Sheets
If Not Sheet.Name = "Prompt" Then
Sheet.Visible = xlSheetVeryHidden
End If
Next
'
If .[A100] = "Saved" Then
.[A100].ClearContents
ThisWorkbook.Save
End If
'
Set Sheet = Nothing
End With
'
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Cancel = True
MsgBox "You cannot save this!"
End Sub

Signature

nobbyknownowt

Bob Phillips - 19 Feb 2007 19:38 GMT
This code worked for me. You need to put it all in the ThosWorkbook code
module.

Private Sub Workbook_Open()

   With Application
       'disable the ESC key
       .EnableCancelKey = xlDisabled
       .ScreenUpdating = False

       Call UnhideSheets

       .ScreenUpdating = True
       're-enable ESC key
       .EnableCancelKey = xlInterrupt
   End With

End Sub
'
Private Sub UnhideSheets()
'
Dim Sheet As Object
'
   For Each Sheet In Sheets
       If Not Sheet.Name = "Prompt" Then
           Sheet.Visible = xlSheetVisible
       End If
   Next
   '
   Sheets("Prompt").Visible = xlSheetVeryHidden
   '
   Set Sheet = Nothing
   ActiveWorkbook.Saved = True

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   With Application
       .EnableCancelKey = xlDisabled
       .ScreenUpdating = False

       Call HideSheets

       .ScreenUpdating = True
       .EnableCancelKey = xlInterrupt
   End With
End Sub

Private Sub HideSheets()
'
Dim Sheet As Object      '< Includes worksheets and chartsheets
'
   With Sheets("Prompt")
       '
       'the hiding of the sheets constitutes a
       'change that generates an automatic "Save?" _
       'prompt, so IF the book has already been
       'saved prior to this point, the next line and the Lines
       'relating to .[A100] below bypass the "Save?" dialog...
       If ThisWorkbook.Saved = True Then .[A100] = "Saved"
       '
       .Visible = xlSheetVisible
       '
       For Each Sheet In Sheets
           If Not Sheet.Name = "Prompt" Then
               Sheet.Visible = xlSheetVeryHidden
           End If
       Next
       '
       If .[A100] = "Saved" Then
           [A100].ClearContents
           ThisWorkbook.Save
       End If
       '
       Set Sheet = Nothing
   End With
   '
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
'    Cancel = True
'    MsgBox "You cannot save this!"
End Sub

Signature

---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

> Is there any specific order that VBA should be put? I have a workbook
> which I want users not to be able to save. I have code to hide the
[quoted text clipped - 91 lines]
> MsgBox "You cannot save this!"
> End Sub
Bob Phillips - 19 Feb 2007 21:14 GMT
This code worked for me. You need to put it all in the ThosWorkbook code
module.

Private Sub Workbook_Open()

   With Application
       'disable the ESC key
       .EnableCancelKey = xlDisabled
       .ScreenUpdating = False

       Call UnhideSheets

       .ScreenUpdating = True
       're-enable ESC key
       .EnableCancelKey = xlInterrupt
   End With

End Sub
'
Private Sub UnhideSheets()
'
Dim Sheet As Object
'
   For Each Sheet In Sheets
       If Not Sheet.Name = "Prompt" Then
           Sheet.Visible = xlSheetVisible
       End If
   Next
   '
   Sheets("Prompt").Visible = xlSheetVeryHidden
   '
   Set Sheet = Nothing
   ActiveWorkbook.Saved = True

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   With Application
       .EnableCancelKey = xlDisabled
       .ScreenUpdating = False

       Call HideSheets

       .ScreenUpdating = True
       .EnableCancelKey = xlInterrupt
   End With
End Sub

Private Sub HideSheets()
'
Dim Sheet As Object      '< Includes worksheets and chartsheets
'
   With Sheets("Prompt")
       '
       'the hiding of the sheets constitutes a
       'change that generates an automatic "Save?" _
       'prompt, so IF the book has already been
       'saved prior to this point, the next line and the Lines
       'relating to .[A100] below bypass the "Save?" dialog...
       If ThisWorkbook.Saved = True Then .[A100] = "Saved"
       '
       .Visible = xlSheetVisible
       '
       For Each Sheet In Sheets
           If Not Sheet.Name = "Prompt" Then
               Sheet.Visible = xlSheetVeryHidden
           End If
       Next
       '
       If .[A100] = "Saved" Then
           [A100].ClearContents
           ThisWorkbook.Save
       End If
       '
       Set Sheet = Nothing
   End With
   '
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
'    Cancel = True
'    MsgBox "You cannot save this!"
End Sub

Signature

---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

> Is there any specific order that VBA should be put? I have a workbook
> which I want users not to be able to save. I have code to hide the
[quoted text clipped - 91 lines]
> MsgBox "You cannot save this!"
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.