2012-05-01 105 views
2

我有一個由多個用戶編輯的電子表格。爲防止篡改以前的數據,一旦輸入數據並保存文件,單元就被鎖定。我在雖然代碼的一些小錯誤:數據輸入後鎖定單元格

  1. 即使用戶手動保存,然後退出,他們依然再次提示保存應用程序。

  2. 當應用程序正在運行時,單元格應該在保存後鎖定,而不是僅在退出時單元格被鎖定。以前,我在before_save事件中使用了這段代碼,但即使save_as事件被取消,單元格也被鎖定,因此我現在刪除了代碼。 固定

(編輯:!我剛剛意識到這個錯誤多麼明顯是我甚至表示,在此聲明試圖鎖定單元格後使用前保存事件部分保存事件)

代碼

With ActiveSheet 
    .Unprotect Password:="oVc0obr02WpXeZGy" 
    .Cells.Locked = False 
    For Each Cell In ActiveSheet.UsedRange 
     If Cell.Value = "" Then 
      Cell.Locked = False 
     Else 
      Cell.Locked = True 
     End If 
    Next Cell 
    .Protect Password:="oVc0obr02WpXeZGy" 
End With 

工作簿開放,隱藏所有的片材,並顯示潛艇用於最終用戶強迫使宏所有片材。下面是完整的代碼:

Option Explicit 
Const WelcomePage = "Macros" 

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 

    Dim ws As Worksheet 
    Dim wsActive As Worksheet 
    Dim vFilename As Variant 
    Dim bSaved As Boolean 

'Turn off screen updating 
    With Application 
     .EnableEvents = False 
     .ScreenUpdating = False 
    End With 

'Record active worksheet 
Set wsActive = ActiveSheet 

'Prompt for Save As 
If SaveAsUI = True Then 
    vFilename = Application.GetSaveAsFilename("", fileFilter:="Excel Files (*.xls), *.xls") 
    If CStr(vFilename) = "False" Then 
     bSaved = False 
    Else 
     'Save the workbook using the supplied filename 
     Call HideAllSheets 
     ThisWorkbook.SaveAs vFilename 
     Application.RecentFiles.Add vFilename 
     Call ShowAllSheets 
     bSaved = True 
    End If 
Else 
    'Save the workbook 
    Call HideAllSheets 
    ThisWorkbook.Save 
    Call ShowAllSheets 
    bSaved = True 
End If 


'Restore file to where user was 
wsActive.Activate 
'Restore screen updates 
With Application 
    .ScreenUpdating = True 
    .EnableEvents = True 
End With 

'Set application states appropriately 
If bSaved Then 
    ThisWorkbook.Saved = True 
    Cancel = True 
Else 
    Cancel = True 
End If 

End Sub 

Private Sub Workbook_Open() 
    Application.ScreenUpdating = False 
    Call ShowAllSheets 
    Application.ScreenUpdating = True 
    ThisWorkbook.Saved = True 
End Sub 

Private Sub HideAllSheets() 
    Dim ws As Worksheet 
    Worksheets(WelcomePage).Visible = xlSheetVisible 
    For Each ws In ThisWorkbook.Worksheets 
     If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden 
    Next ws 
    Worksheets(WelcomePage).Activate 
End Sub 

Private Sub ShowAllSheets() 
    Dim ws As Worksheet 
    For Each ws In ThisWorkbook.Worksheets 
     If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible 
    Next ws 
    Worksheets(WelcomePage).Visible = xlSheetVeryHidden 
End Sub 

'Lock Cells upon exit save if data has been entered 
Private Sub Workbook_BeforeClose(Cancel As Boolean) 
Dim Cell As Range 
With ActiveSheet 
    .Unprotect Password:="oVc0obr02WpXeZGy" 
    .Cells.Locked = False 
    For Each Cell In ActiveSheet.UsedRange 
     If Cell.Value = "" Then 
      Cell.Locked = False 
     Else 
      Cell.Locked = True 
     End If 
    Next Cell 
    .Protect Password:="oVc0obr02WpXeZGy" 
End With 
End Sub 

謝謝:)

回答

1

這是要求他們,即使他們已經保存,因爲這些線路的退出之前保存:

'Save the workbook 
Call HideAllSheets 
ThisWorkbook.Save 
Call ShowAllSheets 
bSaved = True 

你正在改變工作表保存後(通過調用ShowAllSheets),因此需要再次保存。 saveAs代碼也是如此。

0

我使用另一個IF修復了第二個問題。這確保了只有在保存數據時單元才被鎖定:

'Lock Cells before save if data has been entered 
    Dim rpcell As Range 
With ActiveSheet 
    If bSaved = True Then 
    .Unprotect Password:="oVc0obr02WpXeZGy" 
    .Cells.Locked = False 
    For Each rpcell In ActiveSheet.UsedRange 
     If rpcell.Value = "" Then 
      rpcell.Locked = False 
     Else 
      rpcell.Locked = True 
     End If 
    Next rpcell 
    .Protect Password:="oVc0obr02WpXeZGy" 
    Else 
    MsgBox "The LogBook was not saved. You are free to edit the RP Log again", vbOKOnly, "LogBook Not Saved" 
    End If 
End With 
相關問題