Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Before Close workbook?

I have protected sheet and workbook with password.
I have this code and is working ok. Does anyone have some suggestions to
this code, as I am beginner?

Private Sub Workbook_BeforeClose(cancel As Boolean)
Dim L As Long
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect (5)
Me.Worksheets(1).Visible = True
For L = 2 To Me.Worksheets.Count
Me.Worksheets(L).Visible = xlVeryHidden
Next

ActiveWorkbook.Protect Password:="5", Structu=True, Windows:=False
ActiveWorkbook.Save
End Sub

similar is for Open


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Before Close workbook?

Your code looks fine to me. And if it is working, it must be fine. Don't
think you can do it any easier or faster. Only one suggestion and it is more
an issue of developing coding practices that plan for all possible problems
than any actual anticipated problem with your code:

When referring to a collection (where there are multiple objects, like the
WorkSheets(), etc) and you need to refer to a particular one repeatedly it is
better to refer to it by name than by number. The numbering can change if
there are any deletions/insertions. So instead of the For... Next loop, I
think I would have done it like this:

Dim ThisWorksheet as Worksheet

For each ThisWorksheet in ThisWorkbook.Worksheets
If ThisWorksheet.Name<"FirstSheet" Then ThisWorksheet.Visible =
xlVeryHidden
Next ThisWorksheet

"Marc" wrote:

I have protected sheet and workbook with password.
I have this code and is working ok. Does anyone have some suggestions to
this code, as I am beginner?

Private Sub Workbook_BeforeClose(cancel As Boolean)
Dim L As Long
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect (5)
Me.Worksheets(1).Visible = True
For L = 2 To Me.Worksheets.Count
Me.Worksheets(L).Visible = xlVeryHidden
Next

ActiveWorkbook.Protect Password:="5", Structu=True, Windows:=False
ActiveWorkbook.Save
End Sub

similar is for Open



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
close only one workbook bobsmom70601 New Users to Excel 1 March 8th 05 09:43 PM
to close a workbook via VBA J_J[_2_] Excel Programming 3 February 20th 05 01:11 PM
Close a the current workbook and load another specified workbook Adrian[_7_] Excel Programming 4 August 7th 04 05:29 PM
Close a workbook Mike G. Excel Programming 1 June 1st 04 06:50 PM
How can I close a workbook using VBA? Trevor[_4_] Excel Programming 2 March 2nd 04 01:12 AM


All times are GMT +1. The time now is 04:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"