简体   繁体   中英

EXCEL VBA Set timer to protect/lock worksheet with condition(s)

As mentioned in title, what I did was:

Private Sub Workbook_Open()
    Dim ws As Worksheet
    Dim pwd As String
    pwd = "jfm" ' Put your password here
    For Each ws In Worksheets
    ws.Protect Password:=pwd, UserInterFaceOnly:=True
    Next ws
End Sub

But what I want is:

  1. Create a timer, let say 5 seconds
  2. Check if the worksheet is unprotect, protect the sheet after 5 seconds.
  3. If the worksheet is protected then repeat to check if the sheet is unprotect.

Thanks in advance.

Something like:

Dim StartTime As Double

StartTime = Timer

Dim i As Integer
Do Until False

    'if 5 seconds have elapsed, exit loop
    If CInt(Timer - StartTime) > 5 Then Exit Do

Loop

MsgBox "Finished after " & CInt(Timer - StartTime) & " seconds"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM