Ask Experts Questions for FREE Help !
Ask
    kvinay_00's Avatar
    kvinay_00 Posts: 36, Reputation: 1
    Junior Member
     
    #1

    Apr 1, 2010, 04:04 AM
    Lock only filled up cells during saving
    I wish to lock the cells in a particular sheet ("CMstr", see attachment) in which user has entered data and balance cells to be remained unlocked.

    The cells should get locked when the user saves the workbook with a Warning Message (like - to check the data entered is correct/ cells will be locked after saving etc).

    Some other conditions also need to be met as per sheet no "2". VB codes for these conditions are available for reference.

    Can anybody help please?

    Thanks in advance.
    Attached Files
  1. File Type: xls New File - PL.xls (415.5 KB, 152 views)
  2. JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Apr 2, 2010, 06:34 AM

    1) Take the code out of the CMstr sheet module.

    2) Replace this macro in your ThisWorkbook module:
    Code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim myWS As Worksheet
    Cancel = True
    
        For Each myWS In Worksheets
            If myWS.Range("t51").Value = "Check Culprit Code Allocation" Then
                MsgBox "Please check culprit code allocation on Sheet No." & myWS.Name
                Exit Sub
            End If
        Next myWS
    
        If SaveAsUI = True Then
            MsgBox "SaveAs not allowed.  Use Save."
            Exit Sub
        End If
        
        If MsgBox("Saving this workbook will lock the entries completed so far and you will no longer be able to edit them. Do you really want to save the workbook now?", vbYesNo) _
            = vbNo Then Exit Sub
            
        Call UpdateAndSaveSheet
    
    End Sub
    3) Add this to the bottom of the code in the ThisWorkbook module:

    Code:
    Private Sub UpdateAndSaveSheet()
    Dim LastRow As Long, ws As Worksheet
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    For Each ws In Sheets(Array("CMstr"))  'extend this array to include all your sheets by name
        ws.Protect UserInterfaceOnly:=True
    '    ws.Protect Password:="vinay", UserInterfaceOnly:=True
        
        LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
        If LastRow < 3 Then GoTo FinishLine
        
        With ws.Range("A3:H" & LastRow)
            .Locked = True
            .Interior.ColorIndex = 37
        End With
    
    Next ws
    
    FinishLine:
        ThisWorkbook.Save
        Application.ScreenUpdating = True
        Application.EnableEvents = True
    End Sub
    kvinay_00's Avatar
    kvinay_00 Posts: 36, Reputation: 1
    Junior Member
     
    #3

    Apr 3, 2010, 01:39 AM

    Thank you JBeaucaire !

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Air lock or hammer lock [ 3 Answers ]

I was just wondering how I can rememdy hammer lock or airlock? I think that is what it is. I just bought this house that has been sitting for 3 months unoccuppied. I connected my washer and when the washer starts to cycle by pulling water to the unit, I hear this bang evertime it purges water. ...

Clicking differnet cells and it selecting all cells [ 2 Answers ]

Just wondered if anyone knew what I did to get ms excel to select all cells from A1 to what ever cell I clicked on. Thought it was sticky keys but it was turned off. Clicked left mouse button and it moved cells then when I clicked other cells it started using that as a new ref point and selecting...


View more questions Search