重新保护Excel工作表,启用自动筛选

时间:2011-09-12 21:42:38

标签: vb.net excel

我正在使用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项目中使用相同的代码时,下拉菜单没有启用。

1 个答案:

答案 0 :(得分:3)

尝试

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
相关问题