Consulting

Results 1 to 9 of 9

Thread: Solved: Code inconsistancey

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location

    Solved: Code inconsistancey

    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:

    [VBA]
    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
    [/VBA]

    Thanks
    I'm here to help! cmyers1032@aol.com

  2. #2
    Mac Moderator VBAX Expert shades's Avatar
    Joined
    May 2004
    Location
    Kansas City, USA
    Posts
    638
    Location
    Does every workbook have Sheet10? That doesn't seem to be the normal way to refer to sheets.

    Software: LibreOffice 3.3 on Mac OS X 10.6.5
    (retired Excel 2003 user, 3.28.2008 )
    Humanware: Older than dirt
    --------------------
    old, slow, and confused
    but at least I'm inconsistent!

    Rich

  3. #3
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location
    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.
    I'm here to help! cmyers1032@aol.com

  4. #4
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey viper,
    Here's a sample code to check if the worksheet is protected or not (using Sheet1):
    [VBA] Sub Check()
    If Sheet1.ProtectContents = True Then
    MsgBox "true"
    Else
    MsgBox "false"
    End If
    End Sub

    [/VBA]
    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... )




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  5. #5
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location
    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.
    I'm here to help! cmyers1032@aol.com

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Sounds like a VBA file corruption on one of the hard-drives. Try defragging and/or "detect and repair"
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location
    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.
    I'm here to help! cmyers1032@aol.com

  8. #8
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    For the worksheet?[vba]Application.Dialogs(xlDialogProtectDocument).Show[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    VBAX Regular
    Joined
    Nov 2005
    Location
    Jefferson City
    Posts
    13
    Location
    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.

    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.
    I'm here to help! cmyers1032@aol.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •