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

Sub ProtectSpecificCells()
    Dim ws As Worksheet
    Dim protectedRange As Range

    'Set the worksheet where the cells are located
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Set the range of cells to be protected (adjust the range as per your data)
    Set protectedRange = ws.Range("B2Big Grin10")

    'Protect the specified cells with a password
    ws.Protect Password:="mypassword", UserInterfaceOnly:=True
    protectedRange.Locked = True
End Sub

Explanation:

The code defines a subroutine named ProtectSpecificCells.
It declares variables for the worksheet (ws) and the range of cells to be protected (protectedRange).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the cells are located.
Set protectedRange = ws.Range("B2Big Grin10") sets the range of cells B2 to D10 to be protected.
The ws.Protect method is used to protect the entire worksheet with a password ("mypassword"). 
The UserInterfaceOnly:=True argument allows VBA code to modify protected cells without unprotecting the worksheet.
protectedRange.Locked = True locks the specified cells within the protected worksheet.
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
  Format cells in a range with bold font and yellow background color Qomplainerz 0 335 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Split text in cells with VBA Qomplainerz 0 267 07-27-2023, 11:09 AM
Last Post: Qomplainerz
  Count colored cells with VBA Qomplainerz 0 223 07-27-2023, 11:07 AM
Last Post: Qomplainerz
  Calculate the avg and color the cells when their values are above or below the avg Qomplainerz 0 267 07-27-2023, 10:41 AM
Last Post: Qomplainerz
  Find a specific value in a range and replace it with a new value Qomplainerz 0 241 07-27-2023, 10:38 AM
Last Post: Qomplainerz
  Protect and unprotect worksheet with VBA Qomplainerz 0 210 07-27-2023, 08:20 AM
Last Post: Qomplainerz
  Format cells with conditional formatting in VBA Qomplainerz 0 239 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Loop to fill numbers in cells with VBA Qomplainerz 0 228 07-27-2023, 08:16 AM
Last Post: Qomplainerz
  Formatting cells with VBA Qomplainerz 0 213 07-27-2023, 06:31 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)