Excel 2013工作簿\u BeforeSave在Excel 2016中生成错误
该宏在Excel 2013中工作,但现在我已更新到2016年,它不再工作。这意味着,如果工作簿中的多个工作表已填写,则可以在这些工作表中锁定单元格Excel 2013工作簿\u BeforeSave在Excel 2016中生成错误,excel,vba,excel-2016,Excel,Vba,Excel 2016,该宏在Excel 2013中工作,但现在我已更新到2016年,它不再工作。这意味着,如果工作簿中的多个工作表已填写,则可以在这些工作表中锁定单元格 Private Sub Workbook_BeforeSave() 'Resume to next line if any error occurs On Error Resume Next Dim WS_Count As Integer Dim I As Integer Dim Cell As Range
Private Sub Workbook_BeforeSave()
'Resume to next line if any error occurs
On Error Resume Next
Dim WS_Count As Integer
Dim I As Integer
Dim Cell As Range
'Set WS_Count equal to the number of worksheets in the active workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
'loop through all of the Worksheets
For I = 1 To WS_Count
With ActiveWorkbook.Worksheets(I)
'first of all unprotect the entire sheet and unlock all cells
.Unprotect Password:="open"
.Cells.Locked = False
'Now search for non blank cells and lock them
'unlock blank cells
For Each Cell In ActiveWorkbook.Worksheets(I).UsedRange
If Cell.Value > "" Then
Cell.Locked = True
Else
Cell.Locked = False
End If
Next Cell
'Now protect the entire sheet
.Protect Password:="open"
End With
Next I
Exit Sub
End Sub
在Excel 2016中,删除错误恢复下一步时的
将在单元格上出错。Locked=True
,工作簿BeforeSave方法需要附加的非可选参数
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
您需要在方法声明中包含这些参数,即使代码忽略它们。在Excel 2016中,工作簿BeforeSave方法需要附加的非可选参数
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
您需要在方法声明中包含这些,即使代码忽略它们。我解决了问题Noob错误,我在模块中而不是工作簿中使用了宏。我解决了问题Noob错误,我在模块中而不是工作簿中使用了宏。删除“出错时继续下一步”告诉我们它到底在哪一行出错!它在单元格上出错。Locked=TrueDelete“出错时继续下一步”,并告诉我们它实际上在哪一行出错!它在单元格上出错。Locked=true当我重新添加方法时,它不再识别宏。当我重新添加方法时,它不再识别宏。