Filed under: Excel VBA 範例, Workbook

工作表不能編輯及查看公式

by chijanzen on 五月 23rd, 2009 | View: 2,556 views

Tags
  • No Tags
Share Comments (0)
索   引 D0015
主   題 工作表不能編輯及查看公式
版   本 >= 10.0(Office 2003)
說   明 本範例使用保護工作表的方法讓使用者無法編輯及查看公式,但是允許所有的編輯功能(如複製、調整列高、插入列、刪除列..等)
參   考  


複製以下程式碼到ThisWorkbook

Code

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    SetFormulaOn
End Sub
 


Private Sub Workbook_Open()
    SetFormulaHidden
End Sub
 

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, _
        ByVal Target As Range, Cancel As Boolean)
    Cancel = True
End Sub

複製以下程式碼到Module

Code

Sub SetFormulaHidden()
'刪除先前定義的允許使用者可編輯範圍(Title:="Range1")
    SetFormulaOn
    '工作表處於保護狀態時仍能修改
    Cells.Locked = False
    '工作表處於保護狀態時隱藏公式
    Cells.FormulaHidden = True
    '可在受保護工作表中編輯所有儲存格
    ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Cells
    '保護工作表,但是允許所有的編輯功能
    ActiveSheet.Protect _
    AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                        AllowFormattingRows:=True, AllowInsertingColumns:=True, _
                        AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _
                        AllowDeletingColumns:=True, AllowDeletingRows:=True, _
                        AllowSorting:=True, AllowFiltering:=True, _
                        Password:="chijanzen"
End Sub
 


Sub SetFormulaOn()
    On Error Resume Next
    ActiveSheet.Unprotect ("chijanzen")
    ActiveSheet.Protection.AllowEditRanges("Range1").Delete
    Error = 0
End Sub
 

File download

Code

  ActiveSheet.Protection.AllowEditRanges("Range1").Delete

          刪除允許編輯儲存格的鎖定範圍

    

   

File download

Code

檔案下載

Popularity: 37%

About the Author
    網路化名: chijanzen、中國龍、邪兵衛 經歷: 第二屆微軟社群之星 第三屆微軟「最有價值專家」 第五屆微軟「最有價值專家」
No comments currently exist for this post.

Why don't you make one?

Get a GravatarLeave a Reply

Name: « Required

Email Address: « Required

Website URL: « Optional

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me
chijanzen 分享個人Excel VBA 學習經驗,架站心得, 日常生活記事等...

Add to Google

分類
Translator
Chinese (Simplified) flagItalian flagKorean flagEnglish flagGerman flag
French flagJapanese flagRussian flagBulgarian flagFinnish flag
相簿