Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 454
Default Run on Opening Workbook Q

I have the following procedure run when I open up a particular file,
which will display for me the user name within Excel. I have the sheet
protected etc so it can't be tampered with. But I'm hitting a Compile,
I just don't understand these With, End With's

Private Sub Workbook_Open()
For Each sh In ActiveWorkbook.Worksheets
On Error Resume Next
sh.Unprotect Password = "1234"
Cells(6, 3).Value = Application.UserName
sh.Protect Password = "1234"
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Run on Opening Workbook Q

Sean,

You're missing the Next Sh statement that is required by the For Each loop.
Moreover, the line of code

Cells(6, 3).Value = Application.UserName

always points to the ActiveSheet, not the SH sheet. A For Each loop does NOT
activate the sheets as it iterates through them. It simply sets its
reference variable to each sheet.

You will also find life easier if you properly indent your code. It makes it
MUCH easier to read and spot problems. You can use Stephen Bullen's Smart
Indenter if you want to format existing code
(http://www.oaltd.co.uk/Indenter/Default.htm). With properly indented code,
you'll immediately be able to spot a missing Next or End With.



Private Sub Workbook_Open()
Dim SH As Worksheet
On Error Resume Next
For Each SH In ActiveWorkbook.Worksheets
SH.Unprotect Password = "1234"
SH.Cells(6, 3).Value = Application.UserName
SH.Protect Password = "1234"
Next SH
End Sub

Or, using a With statement:

Private Sub Workbook_Open()
Dim SH As Worksheet
On Error Resume Next
For Each SH In ActiveWorkbook.Worksheets
With SH
.Unprotect Password = "1234"
.Cells(6, 3).Value = Application.UserName
.Protect Password = "1234"
End With
Next SH
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)




"Sean" wrote in message
ups.com...
I have the following procedure run when I open up a particular file,
which will display for me the user name within Excel. I have the sheet
protected etc so it can't be tampered with. But I'm hitting a Compile,
I just don't understand these With, End With's

Private Sub Workbook_Open()
For Each sh In ActiveWorkbook.Worksheets
On Error Resume Next
sh.Unprotect Password = "1234"
Cells(6, 3).Value = Application.UserName
sh.Protect Password = "1234"
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Run on Opening Workbook Q

Sean

Need a Next when you have a For

Private Sub Workbook_open()
For Each sh In ActiveWorkbook.Worksheets
On Error Resume Next
With sh
.Unprotect Password = "1234"
.Cells(6, 3).Value = Application.UserName
.Protect Password = "1234"
End With
Next sh
End Sub


Gord Dibben MS Excel MVP

On 14 Jan 2007 09:01:57 -0800, "Sean" wrote:

I have the following procedure run when I open up a particular file,
which will display for me the user name within Excel. I have the sheet
protected etc so it can't be tampered with. But I'm hitting a Compile,
I just don't understand these With, End With's

Private Sub Workbook_Open()
For Each sh In ActiveWorkbook.Worksheets
On Error Resume Next
sh.Unprotect Password = "1234"
Cells(6, 3).Value = Application.UserName
sh.Protect Password = "1234"
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 454
Default Run on Opening Workbook Q

Chip I'm trying to 'manually' unprotect the sheet and it doesn't
recognise the Password, yet its the exact same one thats in the
ThisWorkbook to unprotect - is there something funny happening here? I
only have one sheet in my whole workbook. I've triued caps lock on /
off and still I can't access

Thanks for the Tip on the indent


Chip Pearson wrote:

Sean,

You're missing the Next Sh statement that is required by the For Each loop.
Moreover, the line of code

Cells(6, 3).Value = Application.UserName

always points to the ActiveSheet, not the SH sheet. A For Each loop does NOT
activate the sheets as it iterates through them. It simply sets its
reference variable to each sheet.

You will also find life easier if you properly indent your code. It makes it
MUCH easier to read and spot problems. You can use Stephen Bullen's Smart
Indenter if you want to format existing code
(http://www.oaltd.co.uk/Indenter/Default.htm). With properly indented code,
you'll immediately be able to spot a missing Next or End With.



Private Sub Workbook_Open()
Dim SH As Worksheet
On Error Resume Next
For Each SH In ActiveWorkbook.Worksheets
SH.Unprotect Password = "1234"
SH.Cells(6, 3).Value = Application.UserName
SH.Protect Password = "1234"
Next SH
End Sub

Or, using a With statement:

Private Sub Workbook_Open()
Dim SH As Worksheet
On Error Resume Next
For Each SH In ActiveWorkbook.Worksheets
With SH
.Unprotect Password = "1234"
.Cells(6, 3).Value = Application.UserName
.Protect Password = "1234"
End With
Next SH
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)




"Sean" wrote in message
ups.com...
I have the following procedure run when I open up a particular file,
which will display for me the user name within Excel. I have the sheet
protected etc so it can't be tampered with. But I'm hitting a Compile,
I just don't understand these With, End With's

Private Sub Workbook_Open()
For Each sh In ActiveWorkbook.Worksheets
On Error Resume Next
sh.Unprotect Password = "1234"
Cells(6, 3).Value = Application.UserName
sh.Protect Password = "1234"
End Sub


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
How do I stop blank workbook from opening when opening an existing kjg Excel Discussion (Misc queries) 3 February 12th 10 09:36 PM
excel VBA problem - setting workbook as variable & opening/re-opening safe Excel Programming 1 August 20th 04 12:22 AM
How to make the opening of a workbook conditional upon the opening of another workbook Marcello do Guzman Excel Programming 1 December 16th 03 06:09 AM
How to make opening of workbook conditional of opening of another workbook turk5555[_2_] Excel Programming 2 December 15th 03 11:07 PM
Preventing opening workbook inside active workbook. Serge[_4_] Excel Programming 2 November 4th 03 07:51 PM


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

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

About Us

"It's about Microsoft Excel"