Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 04-06-2012, 10:37 PM
Catalin.B Catalin.B is offline Excel VBA: Worksheet_Change Cells.Protect Windows Vista Excel VBA: Worksheet_Change Cells.Protect Office 2010 32bit
Expert
 
Join Date: May 2011
Location: Iaşi, Romānia
Posts: 386
Catalin.B is on a distinguished road
Default

Another approach i to protect all completed cells when closing the workbook, this way the code will not run at every change, slowing down work.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim LastRow, LastColumn
Dim LastCell As String

Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:="1234"

With ActiveSheet
        If WorksheetFunction.CountA(Cells) > 0 Then
          LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious).Row
        End If
  If WorksheetFunction.CountA(Cells) > 0 Then
    LastColumn = Cells.Find(What:="*", After:=[A1], _
       SearchOrder:=xlByColumns, _
       SearchDirection:=xlPrevious).Column
  End If
    
     LastCell = "A1:" & Split(Columns(LastColumn).Address, "$")(2) & LastRow
   
   .Range(LastCell).Cells.Locked = False

    For Each cell In .Range(LastCell).Cells
         If Not IsEmpty(cell) Then
             cell.Locked = True
         End If
    Next cell

ActiveSheet.Protect Password:="1234", DrawingObjects:=True, Contents:=True, Scenarios:=True
Application.ScreenUpdating = True
End With
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Protect / Lock Cells / Print Set-up? meggenm Excel 3 01-26-2012 09:57 PM
protect and unprotect in shared excel mark-gabb Excel 0 01-09-2012 07:30 PM
Excel 2010 Format cells to use as Gantt or Plan Kizzie Excel 0 12-13-2011 02:31 AM
Excel VBA: Worksheet_Change Cells.Protect password protect excel file? Dawg751 Excel 2 01-28-2010 06:23 AM
Excel VBA: Worksheet_Change Cells.Protect Importing individual cells from Excel to Word themangoagent Word 1 10-12-2009 08:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:37 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft