我正在使用 VB.net 2005 将数据从数据库应用程序输入到 Excel 文件中。
我取消保护工作表,使用命名范围将数据输入到特定单元格,然后重新保护工作表。
到目前为止,这一切都运行良好,直到我遇到一个受启用自动过滤保护的文件。当我重新保护工作表时,我似乎无法在允许使用自动过滤器下拉列表的同时保护它。自动过滤器的下拉箭头显示为灰色并被禁用。
输入数据后使用以下代码重新保护工作表。
If bolProtected = True Then
For i = 0 To intProtectIndexes.Length - 1
objExcel.Sheets(intProtectIndexes(i)).Select()
objExcel.ActiveSheet.Protect(strPassword)
Next
End If
还尝试使用 Tim 链接中的代码。
objExcel.Sheets(intProtectIndexes(i)).Select()
objExcel.ActiveSheet.Protect(Password:=strPassword, Contents:=True)
objExcel.activesheet.enableautofilter = True
这仍然会在填充的工作表上禁用自动过滤器下拉列表。
在 VBA 中创建宏并使用:
ActiveSheet.Protect(Contents:=True, AllowFiltering:=True)
工作正常。该工作表受到保护,我可以使用自动过滤器下拉菜单。但是当我在 VB.net 项目中使用相同的代码时,下拉菜单未启用。
最佳答案
尝试
With objExcel.Sheets(intProtectIndexes(i))
.Protect(Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, UserInterfaceOnly:=True)
.EnableAutoFilter = True
End With
或者
With objExcel.Sheets(intProtectIndexes(i))
.Protect (Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True)
End With
关于vb.net - 重新保护 Excel 工作表,启用自动筛选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7394414/