+ Reply to Thread
Results 1 to 5 of 5

Vba to protect the cell

Hybrid View

  1. #1
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Vba to protect the cell

    Hi I Need a help that when i click on save button the Non Blank
    Cell will protect so that no one can edit the cell content .only the
    Blank cell will edit
    Attached Files Attached Files

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Vba to protect the cell

    The default for cells is locked, so first unlock all the cells.

    Click the empty grey box between the colmn & row headers to select all cells -> Format -> Protection -> uncheck Locked. Then use this code

    Option Explicit
    
    Sub Button1_Click()
        Const PW   As String = "secret"
        Dim rNonBlanks As Range
        On Error Resume Next
        Set rNonBlanks = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
        On Error GoTo 0
        rNonBlanks.Locked = True
        ActiveSheet.Protect PW
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Vba to protect the cell

    The default for cells is locked, so first unlock all the cells.

    Click the empty grey box between the colmn & row headers to select all cells -> Format -> Protection -> uncheck Locked. Then use this code

    Option Explicit
    
    Sub Button1_Click()
        Const PW   As String = "secret"
        Dim rNonBlanks As Range
        On Error Resume Next
        Set rNonBlanks = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
        On Error GoTo 0
        rNonBlanks.Locked = True
        ActiveSheet.Protect PW
    End Sub

  4. #4
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: Vba to protect the cell

    Quote Originally Posted by royUK View Post
    The default for cells is locked, so first unlock all the cells.

    Click the empty grey box between the colmn & row headers to select all cells -> Format -> Protection -> uncheck Locked. Then use this code

    Option Explicit
    
    Sub Button1_Click()
        Const PW   As String = "secret"
        Dim rNonBlanks As Range
        On Error Resume Next
        Set rNonBlanks = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
        On Error GoTo 0
        rNonBlanks.Locked = True
        ActiveSheet.Protect PW
    End Sub
    Thank for reply Roy is that possible we can define the range in code so that macro will run on that range only instead on whole sheet

  5. #5
    Registered User
    Join Date
    11-27-2008
    Location
    Noida
    Posts
    51

    Re: Vba to protect the cell

    please suggest

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1