Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Unprotect Workbook with Function

I unprotect and protect all worksheets in a workbook with a User Defined
Function that returns the password.

Sub MyExample()
ActiveSheet.Unprotect Password()
' do stuff to the worksheet
ActiveSheet.Protect Password()
End Sub

Function Password() As String
Password = "AdTech"
End Function

This works great! But for some reason when I try to unprotect or protect my
workbook I get a Run Time Error. I know the password is "AdTech", because I
can manually unprotect and protect the workbook, but VBA doesn't like to do
it thru code.

Sub MyExample()
ThisWorkbook.Unprotect Password()
' do stuff to the worksheet
ThisWorkbook.Protect Password()
End Sub


Any one have any ideas why?

Thanks in Advance!


--
Cheers,
Ryan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 411
Default Unprotect Workbook with Function

Hi Ryan,

I'm wondering how you get the run time error if the function works
great.

Dan
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Unprotect Workbook with Function

The function works when I want to unprotect/protect worksheets. But it
doesn't work when I want to unprotect/protect the workbook. I'm not sure
why????
--
Cheers,
Ryan


"dan dungan" wrote:

Hi Ryan,

I'm wondering how you get the run time error if the function works
great.

Dan
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Unprotect Workbook with Function

First, I wouldn't use Password for the name of the function. It looks way too
much like the keywords VBA uses. And even though it may not confuse excel, it
surely would confuse me.

Second, I wouldn't include the ()'s in the call to the function.

activesheet.unprotect password:=FuncPWD
ThisWorkbook.Unprotect Password:=FuncPWD

Third, the unmodified code worked fine for me when I ran it--even without my
changes!

So I'm guessing that either the password isn't really what you expect--or
ThisWorkbook isn't the workbook that you expect.


Ryan H wrote:

I unprotect and protect all worksheets in a workbook with a User Defined
Function that returns the password.

Sub MyExample()
ActiveSheet.Unprotect Password()
' do stuff to the worksheet
ActiveSheet.Protect Password()
End Sub

Function Password() As String
Password = "AdTech"
End Function

This works great! But for some reason when I try to unprotect or protect my
workbook I get a Run Time Error. I know the password is "AdTech", because I
can manually unprotect and protect the workbook, but VBA doesn't like to do
it thru code.

Sub MyExample()
ThisWorkbook.Unprotect Password()
' do stuff to the worksheet
ThisWorkbook.Protect Password()
End Sub

Any one have any ideas why?

Thanks in Advance!

--
Cheers,
Ryan


--

Dave Peterson
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
Unprotect workbook Kim Excel Discussion (Misc queries) 3 October 2nd 08 07:49 PM
overriding the Workbook Unprotect-function Puil Excel Programming 2 October 1st 08 04:23 PM
unprotect workbook Deb Excel Discussion (Misc queries) 2 February 9th 07 07:40 PM
Unprotect workbook Jean-Paul De Winter Excel Programming 4 October 17th 04 09:00 AM
Unprotect a Workbook Milind Excel Programming 0 September 10th 03 10:59 PM


All times are GMT +1. The time now is 01:23 PM.

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"