• Protecting Sheet (XL2000 SP3)

    Author
    Topic
    #454485

    I am working on a workbook with a UsageLog sheet. The following writes the time and username when the workbook is saved. It works all right except that the lines:
    ActiveWorkbook.Worksheets(“UsageLog”).Unprotect
    and
    ActiveWorkbook.Worksheets(“UsageLog”).Protect
    Do not seemed to have any effect; if the sheet is unprotected to start with the macro runs properly but the sheet is still unprotected when the macro ends; if the sheet is protected before running the macro it gives an error 1004 at the line
    ThisWorkbook.Worksheets(“UsageLog”).Range(“A65536”).End(xlUp).Offset(0, 3).Value = Time$ & Space(5) & Date$
    Or, if this line is rem’ed out, 2 lines later (the second msgbox shows that ThisWorkbook and ActiveWorkbook are the same, and the macro falls over whichever is used).

    Sub EnterSaveData()
    On Error GoTo ErrorHandler

    MsgBox (“In Macro EnterSaveData”)
    ‘following checks ThisWorkbook and ActiveWorkbook are UsageLogTest.xls
    Dim name$, name2$
    name$ = Application.ThisWorkbook.name
    name2$ = Application.ActiveWorkbook.name
    MsgBox “ThisWorkbook is ” & name$ & Chr(13) & Chr(13) & “ActiveWorkbook is ” & name2$

    Application.ActiveWorkbook.Save

    ActiveWorkbook.Worksheets(“UsageLog”).Unprotect
    MsgBox (“In Macro EnterSaveData, have UnProtected Sheet”)

    ThisWorkbook.Worksheets(“UsageLog”).Range(“A65536”).End(xlUp).Offset(0, 3).Value = Time$ & Space(5) & Date$
    MsgBox (“Should have entered save date but not username”)
    Application.ActiveWorkbook.Worksheets(“UsageLog”).Range(“A65536”).End(xlUp).Offset(0, 4).Value = Environ(“Username”)
    MsgBox (“Should have entered save date and username”)

    ActiveWorkbook.Worksheets(“UsageLog”).Protect
    MsgBox (“In Macro EnterSaveData, have Protected Sheet”)

    Exit Sub

    ErrorHandler:
    MsgBox (“error number”) & Err
    Select Case Err

    Case Is = 9
    MsgBox (“Worksheet UsageLog is missing. Insert & name this sheet. Press OK to exit macro.”)

    Exit Sub

    Case Else
    MsgBox (“Error other than UsageLog sheet missing. Error”) & Err

    End Select

    End Sub

    Can anyone help? TIA

    Viewing 1 reply thread
    Author
    Replies
    • #1128326

      Your code runs without problems when I try it except that it doesn’t enter anything in column A so it writes to the same row in columns D and E each time.
      It works if the sheet UsageLog is unprotected and if the sheet is protected, but of course if the sheet is protected with a password, the code will prompt for the password, and if you cancel the prompt, the code will fail with error code 1004.

      • #1128446

        Thank you Hans

        I am not using a password so that is not the problem. What version of XL are you using? The only think I can think of is that Protect is one of those things that does not work in some versions of XL ( I think TRIM was another though I cannot remember which version it was). If anyone else is still using 2000 SP3 so they could check this.

        Thanks again

        • #1128447

          I tested in Excel 2002 SP3; I don’t have Excel 2000.

        • #1128463

          I tried in with XL2000 (no SPs added) and it seems to work as expected…

          Steve

        • #1128465

          Since Steve (sdckapr) reports that the code works in Excel 2000 too, could you post a sample workbook (with some dummy data) in which the code fails?

          • #1128470

            Hans and Steve

            The problem arose at work where I use XL2000 SP3; at home I have XL2000 SR1 and the code works fine.

            I will not be at work again until next week but I will post the workbook then.

            However suppose my theory is right and there is a glitch in 2000 SR3, if I send a workbook with a macro written with 2000 SR3 and someone with a different version of XL opens it and runs it will not the macro run with the VBA in that person’s version so if that version is glitch-free the problem will not show up? Or am I misunderstanding how macros, workbooks and versions interact?

            • #1128475

              I can’t find any mention of a problem with protecting/unprotecting sheets that is specific to SP3, but I’m sure there will be a Lounger who has Excel 2000 SP3 to test the workbook.

            • #1129590

              My theory that Protect does not work in XL2000 SP3 is wrong. Attached is my workbook with macros that Protect and Unprotect the worksheet, and they work fine as long as they are run alone. But when they are called from the other macros, FormatUsageLog, EnterOpenData, or EnterSaveData they seem to do nothing. If the worksheet is unprotected before it is saved everything is fine, but if the worksheet is protected I get Error 1004. Can anyone tell me what is going on?

            • #1129601

              It has nothing to do with the version of Excel. Your code doesn’t ever call the protect or unprotect code because you have lines such as

              ProtectUsageLog: MsgBox (“In Macro FormatUsageLog have Protected Sheet”)

              ProtectUsageLog is interpreted as a label, not as a call to ProtectUsageLog. You should change it to

              Call ProtectUsageLog: MsgBox (“In Macro FormatUsageLog have Protected Sheet”)

              or to

              ProtectUsageLog
              MsgBox (“In Macro FormatUsageLog have Protected Sheet”)

              and the same for all similar lines. The code will then protect and unprotect the sheet as intended.

            • #1129694

              Hans
              You are right of course, I had used the colon to join the Protect call with its MsgBox so they could be remarked out together, without realising VB would think it indicated a nonexistant destination.

              However here is a version with that error removed and the problem persists.

            • #1129696

              It works OK for me in Excel 2002 SP3. I have opened and closed your workbook a few times, and got all the relevant message boxes; the result is attached. I had to enable macros when I opened the workbook, of course.

            • #1129698

              Hans

              I am at home using XL2000 SR1 and your workbook opens alright for me but crashes when I try to save it.

              Is there someone else using XL2000 who can try Hans’ workbook and see if they can save it?

              Thanks to all.

            • #1129715

              For what it’s worth, the code works correctly in Excel 2003 SP3 too.

            • #1129839

              Hans

              I have now tried your workbook in XL2000 SP3 and it crashes at the save step there too unless I manually unprotect the sheet first.

            • #1129840

              We’ll have to wait until someone else with Excel 2000 can test…

              Rory! megashout

            • #1129844

              I don’t have any problems with Hans’ workbook from in XL2000 (9.0.2720). I opened it and It looped through the messages and put info in the cells.

              I can close (and be prompted to save and accept) or just save and I get a string of messages about what it is doing and info is again stored.

              It does not crash for me…

              Steve

            • #1129876

              Sorry, my router blew a gasket yesterday so I have no internet at home currently, but at least this has been resolved in the meantime!

            • #1129846

              Since there doesn’t seem to be anything wrong with the code, and since Steve (sdckapr) has tested it successfully in Excel 2000, there might be a problem with your installation. Perhaps a misbehaving add-in. Try Jan Karel Pieterse’s Systematic Approach to Behavioral Problems in XL.

            • #1129861

              Thanks to you all for your advice.

              I tried out the book on another machine at work with the same XL version as mine and it worked (should have thought of that earlier).

              The problem was a utility called Saver that I installed years ago that puts the full path and filename in the Caption when a file is saved, I removed this and the problem went away.

              Thanks again everyone for your efforts.

            • #1129774

              I have XL2k at home so I will try it later on if I remember!

    • #1129931

      I like this idea of a usage log and I am adapting your code to a workbook of my own. It is shared and I have always been curious to know when the other parties have been in the workbook. The problem I see with your current code is that if the user closes out of the spreadsheet without saving, then the username and date/time opened is not saved. Is there a way to save the log without forcing the Workbook_BeforeSave code to run?

      • #1129932

        Not in the workbook itself (unless you save it immediately after opening it, but that might not be desirable.)
        You could write the usage information to a text file instead, using the old Open, Print and Close instructions.

        • #1129936

          If I wanted to save it immediately after opening, how would I do that? What might be undesirable about this?

          I tried this code but it started the BeforeSave code which then failed because the protection is password protected.

          Private Sub Workbook_Open()
          With Worksheets(“View”)
          .Unprotect (“xxxxxxx”)
          .EnableOutlining = True
          FormatUsageLog
          EnterOpenData
          .Protect UserInterfaceOnly:=True, Password:=(“xxxxxxx”)
          End With
          ActiveWorkbook.Save

          End Sub

          Thanks in advance,

          • #1129939

            You should add the password to all the other lines that unprotect and protect the worksheet too.

      • #1130614

        I had not thought of this as we want the usage log to see the workbook’s history, who has used it, who has altered it (I intend to add a column recording the last change made to the book, when I have the open and save bits working properly) etc. However I think you are right, it could be useful to know when someone has just looked at the book and then closed it without saving it, I added the following lines to the EnterOpenData macro after the writing of the opening date/time and username and before the reprotection of the sheet.

        Application.EnableEvents = False
        ActiveWorkbook.Save
        Application.EnableEvents = True

        This seems to work OK, if the workbook is closed without saving there are blank cells alongside the opening data.

        • #1130675

          Thank you Michael!

          That did exactly what I wanted it to do. When you have the code for recording changes I would also be interested in that.

          The workbooks I am using this in contain budget information that is provided by my office to managers of the individual budgets. It helps us in scheduling budget meetings to know when and if they have reviewed the reports or have made entries that we ned to act on.

    Viewing 1 reply thread
    Reply To: Protecting Sheet (XL2000 SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: