Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Protect and unprotect worksheet with VBA
#1
Example:

Sub ProtectUnprotectWorksheet()
    Dim ws As Worksheet
    Dim password As String

    'Set the password for protection
    password = "mypassword"

    'Get the reference to the active worksheet
    Set ws = ActiveSheet

    'Protect the worksheet with password
    ws.Protect Password:=password

    'Wait for 2 seconds
    Application.Wait (Now + TimeValue("00:00:02"))

    'Unprotect the worksheet with password
    ws.Unprotect Password:=password
End Sub

Explanation:

The code defines a subroutine named ProtectUnprotectWorksheet.
It declares a variable ws of type Worksheet and a variable password to store the password used for protection.
The password "mypassword" is set using the password variable.
The ActiveSheet property is used to get a reference to the currently active worksheet, which will be protected and unprotected.
The Protect method is used to protect the worksheet with the specified password.
The Application.Wait function is used to introduce a delay of 2 seconds before unprotecting the worksheet. This allows time to observe the protected state.
Finally, the Unprotect method is used to unprotect the worksheet with the same password.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy data to another worksheet Qomplainerz 0 253 07-27-2023, 11:24 AM
Last Post: Qomplainerz
  Protect specific cells with VBA Qomplainerz 0 259 07-27-2023, 10:39 AM
Last Post: Qomplainerz
  Create a pivot table based on data in a worksheet with VBA Qomplainerz 0 276 07-27-2023, 09:48 AM
Last Post: Qomplainerz
  Filter data in a worksheet Qomplainerz 0 253 07-27-2023, 09:46 AM
Last Post: Qomplainerz
  Create a new worksheet and rename it with VBA Qomplainerz 0 215 07-27-2023, 08:14 AM
Last Post: Qomplainerz
  Count the number of rows in a worksheet with VBA Qomplainerz 0 231 07-27-2023, 07:50 AM
Last Post: Qomplainerz
  Worksheet methods in VBA Qomplainerz 0 178 07-26-2023, 08:44 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)