PDA

View Full Version : Solved: Code inconsistancey



viper
11-21-2005, 09:12 AM
I use this code on my laptop and it works fine. Using it on my works computer I get an error on the sheet unprotect line. Is there a different way to write it so it will work on all computers?

I've used:

Sheets("Labor worksheet").UnProtect password:="password"
Sheets("Labor worksheet").UnProtect "password"

and then this:


Dim psword, pword
pword = "password"
psword = InputBox("Enter password to make changes")
If psword <> pword Then
MsgBox "Sorry, incorrect password"

Exit Sub
End If

If psword = pword Then
ThisWorkbook.Unprotect password:="thisbook"
Sheet10.Unprotect password:="viper" 'error line
UnHideColumn
End If


Thanks

shades
11-21-2005, 09:33 AM
Does every workbook have Sheet10? That doesn't seem to be the normal way to refer to sheets.

viper
11-22-2005, 12:53 PM
No not every workbook has a sheet10. But the activeworkbook does. Like I said, I get this error only on one computer. I've even changed the code to ActiveSheet.Unprotect password:="" and get a runtime error 1004. Makes no sense to me.

The reverse has no errors and I use Sheet10.Protect password:=""

Is there a way that I can check if the sheet is protected then if it is unprotect it? I've tried:

If Activesheet.Protection = False Then Activesheet.Unprotect password:="" but again got a doesn't support this error.

malik641
11-22-2005, 01:39 PM
Hey viper,
Here's a sample code to check if the worksheet is protected or not (using Sheet1):
Sub Check()
If Sheet1.ProtectContents = True Then
MsgBox "true"
Else
MsgBox "false"
End If
End Sub


And for the error code you are getting....when you get the run-time 1004 error, does it also say that the password you provided is incorrect?

If somebody changed the password after it was unprotected and saved the file, you will get an error. (But then again, you said it works fine on your computer...:think: )

viper
11-22-2005, 05:19 PM
Thanks,

No the password is the same, if I use the Tools|Unprotect Worksheet it will unprotect with the password stored in the code. It's just really wierd that it does this.

johnske
11-22-2005, 08:18 PM
Sounds like a VBA file corruption on one of the hard-drives. Try defragging and/or "detect and repair"

viper
11-23-2005, 02:06 PM
Is there a way to bring up the password dialog box? Unfortunately I cannot do the detect and repair since this is on a corporate computer. As long as I enter the password using Tools|Unprotect it works fine just not with code.

johnske
11-23-2005, 02:24 PM
For the worksheet?Application.Dialogs(xlDialogProtectDocument).Show

viper
11-23-2005, 08:43 PM
Thanks for all your help. I had to go a different route. I really don't quite understand what is going on but I didn't supply a password in the code and just protected it without a password. Worked fine when the code unprotected the sheet without a password. When the code protected the worksheet, the code couldn't unprotect it. :dunno

So I just hid all forumula's and set the scrollarea upon the workbook opening for that worksheet so at least the outside formulas are safe. The rest should be okay.

Again thanks for all the help.:bow: