vb代码控制 Excel锁定单元格

简介:
Sub Protect([Password], [DrawingObjects], [Contents], [Scenarios], [UserInterfaceOnly], [AllowFormattingCells], [AllowFormattingColumns], [AllowFormattingRows], [AllowInsertingColumns], [AllowInsertingRows], [AllowInsertingHyperlinks], [AllowDeletingColumns], [AllowDeletingRows], [AllowSorting], [AllowFiltering], [AllowUsingPivotTables])
    Member of Excel.Worksheet

如下代码:
Public Function SaveToExcel() As Boolean
On Error GoTo SaveToExcelErr
   Dim fs As Scripting.FileSystemObject, f As File
   Dim checkNow As Boolean
   Dim objWorkSheet As Worksheet
   Dim objworkBook As Workbook
   Dim objExcel As Excel.Application
   Dim intI As Integer
   Dim rang As Range
   Dim arrTemp() As String
   Dim intRow As Integer
   Dim intX As Integer
   Dim filename As String
    Set objExcel = New Excel.Application       
    Set objworkBook = objExcel.Workbooks.add    
    Set objWorkSheet = objworkBook.Worksheets.item(1)    
   'Write Header
   For intRow = 1 To 3
      'intRow = 65
       'intX = 1
       arrTemp = Split(arrTagExcel(intRow - 1), "|")
       For intX = LBound(arrTemp) + 1 To UBound(arrTemp) + 1
                    'With objWorkSheet.Range(Chr(intRow) & CStr(intX + 1))
                   With objWorkSheet.Cells(intRow, intX)
                   If intRow = 1 Then .Font.size = 13
                   If intRow = 3 Then .Interior.Pattern = xlPatternGray25
                   .Font.Bold = True
                   .value = arrTemp(intX - 1)
                   .ColumnWidth = Len(arrTemp(intX - 1)) + 1.5
                   .Locked = True
                  End With                 
       Next
    Next
    'Write Content
    'rows
    For intRow = 4 To UBound(arrTagExcel)       
        arrTemp = Split(arrTagExcel(intRow - 1), "|")
       For intX = LBound(arrTemp) + 1 To UBound(arrTemp) + 1           
                  'objWorkSheet.Range(Chr(intRow) & CStr(intX + 1)).NumberFormatLocal = "@" ' .Select.Activate = arrtemp(intI)
'                    If intX = UBound(arrtemp) + 1 Then
'                       Debug.Print "ok"
'                    End If
                    objWorkSheet.Cells(intRow, intX) = arrTemp(intX - 1)                 
                   'objWorkSheet.Range(intRow, intX + 1).Locked = False
                   objWorkSheet.Cells(intRow, intX).Locked = False
                   objWorkSheet.Cells(intRow, intX).NumberFormatLocal = "@"
                   'objWorkSheet.Cells(intRow, intX)                   
       Next
       'intX = intX + 1
       objWorkSheet.Cells(intRow, intX - 1).Font.Color = vbRed
    Next
      objWorkSheet.Protect "", False, True, False, False, True, True, True, False, False, False, False, True, False, False, True 
      'objWorkSheet.Protect

    lblstatus = "Finished!"    
       Set fs = New Scripting.FileSystemObject
        If fs.FileExists(txtFileName) Then
               If MsgBox("Excel file already exists,Do you want to replace it.", vbOKCancel + vbQuestion, "Finish") = vbOK Then
                     fs.DeleteFile txtFileName, True
               Else
                     With CommonDialog1
                        .DialogTitle = "Please choose another directory..."
                        .ShowSave
                        If (.filename <> "") Then filename = .filename
                        If InStr(filename, ".xls") > 0 Then filename = Left(filename, InStr(filename, ".xls") - 1)
                          txtFileName.Text = filename + ".xls"
                        End With                     
               End If
        End If
        Set f = Nothing
        Set fs = Nothing        
        objworkBook.SaveAs txtFileName
        objExcel.Visible = True
        SaveToExcel = True
    Exit Function
SaveToExcelErr:
'   If Not objExcel Is Nothing Then
'     objExcel.Workbooks.Close
'     objExcel.Quit
'     Set objExcel = Nothing
'   End If
If Err.number = 70 Then
   Set itmX = lvwRst.ListItems.add(, , Err.Source + ":Save Permission Denied!The excel file has been opened already.", , 2)
Else
   Set itmX = lvwRst.ListItems.add(, , Err.Source + ":" + Err.Description, , 2)
End If
    
    
End Function


如果为     objWorkSheet.Protect "", False, True, False, False, True, True, True, False, False, False, False, True, False, False, True 
则结果入 附件1 :
如果为      'objWorkSheet.Protect
则结果如 附件2:


Sub Protect([Password], [DrawingObjects], [Contents], [Scenarios], [UserInterfaceOnly], [AllowFormattingCells], [AllowFormattingColumns], [AllowFormattingRows], [AllowInsertingColumns], [AllowInsertingRows], [AllowInsertingHyperlinks], [AllowDeletingColumns], [AllowDeletingRows], [AllowSorting], [AllowFiltering], [AllowUsingPivotTables])
中每个参数什么意思真的还不清楚!
[Password]                                :设置保护密码
[DrawingObjects]                       :是否允许修改excel sheet上面的图形之类的物件
[Contents]                                 :是否允许修改内容
[Scenarios]                               :?
[UserInterfaceOnly]                    : ?
[AllowFormattingCells]               :是否允许Formatting单元格(就是是否允许对单元格进行拖拉了)
[AllowFormattingColumns]         :是否允许Formatting列
[AllowFormattingRows]              :是否允许Formatting行
[AllowInsertingColumns]            :是否允许插入列
[AllowInsertingRows]                 :是否允许插入行
[AllowInsertingHyperlinks]          :是否允许超链接
[AllowDeletingColumns]             :是否允许删除列
[AllowDeletingRows]                  :是否允许删除行
[AllowSorting]                           :是否允许排序
[AllowFiltering]                          :是否允许过滤
[AllowUsingPivotTables]             :?



http://www.microsoft.com/china/msdn/library/office/office/UndstaExcelObjModNETDev.mspx?mfr=true 中得知:

设置 Password 参数来指定一个区分大小写的字符串,这是取消保护工作表所需要的。如果您不指定这个参数,任何人都可以取消保护工作表。
将 Contents 参数设置为 True 来保护工作表的内容(单元格)。默认值为 True,您可能永远不会改变它。 
将 DrawingObjects 参数设置为 True 来保护工作表的形状。默认值为 False。 
将 Scenarios 参数设置为 True 来保护工作表中的方案。默认值为 True。
将 UserInterfaceOnly 参数设置为 True 可以允许通过代码修改,但是不允许通过用户界面修改。默认值为 False,这意味着通过代码和用户界面项都不可以修改受保护的工作表。这个属性设置只适用于当前会话。如果您想让代码可以在任何会话中都可以操作工作表,那么您需要每次工作簿打开的时候添加设置这个属性的代码。

AllowFormattingCells 参数、AllowFormattingColumns 参数和前面方法语法的完整列表中所示的其余参数允许特定的格式化功能,对应于对话框中(如图 12 所示)的选项。默认情况下,所有这些属性都是 False。 

本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2006/05/31/414054.html如需转载请自行联系原作者


kenty

相关文章
excel中同一单元格内容分隔到不同的单元格
excel中同一单元格内容分隔到不同的单元格
excel中同一单元格内容分隔到不同的单元格
|
6天前
|
BI 索引 Python
python报表自动化系列 - 译码:与Excel单元格索引对应的十进制数坐标
python报表自动化系列 - 译码:与Excel单元格索引对应的十进制数坐标
17 1
|
6月前
|
Python
python openpyxl 打开excel 并打印 七行代码
python openpyxl 打开excel 并打印 七行代码
43 0
|
7月前
|
Java Maven
【Java用法】使用poi写Java代码导出Excel文档的解决方案
【Java用法】使用poi写Java代码导出Excel文档的解决方案
59 0
|
6天前
|
BI 索引 Python
python报表自动化系列 - Excel单元格(Cell)索引范围对应的所有单元格
python报表自动化系列 - Excel单元格(Cell)索引范围对应的所有单元格
29 0
|
5天前
|
Java Apache 索引
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
|
6天前
|
数据处理
在什么情况下,Excel 单元格内会显示 #VALUE
在什么情况下,Excel 单元格内会显示 #VALUE
13 2
|
6天前
|
开发者 Python
如何让excel 运行python代码
如何让excel 运行python代码
|
6天前
|
JavaScript
【sgExcelGrid】自定义组件:简单模拟Excel表格拖拽、选中单元格、横行、纵列、拖拽圈选等操作
【sgExcelGrid】自定义组件:简单模拟Excel表格拖拽、选中单元格、横行、纵列、拖拽圈选等操作
【sgExcelGrid】自定义组件:简单模拟Excel表格拖拽、选中单元格、横行、纵列、拖拽圈选等操作