2009-01-15 97 views
10

下面的代碼片段更改了單元格的數據驗證狀態,並在Excel-2003工作表不受保護時運行。然而,當我保護工作表中的宏不運行,提高運行時錯誤工作表在MS-Excel中受保護時,如何避免運行時錯誤?

Run-time error '-2147417848 (80010108)':

Method 'Add' of object 'Validation' failed

我已經試過包裝代碼與

Me.unprotect 
... 
Me.protect 

但是,這並不正常工作。那麼,如何在工作表受到保護而沒有上述運行時錯誤的情況下如何修改下面的代碼來工作(即讓代碼修改解鎖單元的驗證)?

更新

我原來的工作簿是Excel 2003,我測試@eJames解決方案在Excel 2007以下定義爲Workbook_Open

Sub WorkBook_Open() 
    Me.Worksheets("MainTable").Protect contents:=True, userinterfaceonly:=True 
End Sub 

的代碼仍然失敗,出現以下運行 - 工作表受到保護時出現實時錯誤

Run-time error '1004': Application-defined or object-defined error

Thanks,Azim


代碼段

'cell to add drop down validation list' 
dim myNamedRange as String 
dim modifyCell as Range 
modifyCell = ActiveCell.Offset(0,1) 


' set list values based on some conditions not defined for brevitity' 
If myCondition then 
    myNamedRange = "range1" 
Else 
    myNamedRange = "range2" 
End If 

With modifyCell.Validation 
    .Delete 

    'Run time error occurs on the next line' 
    .Add Type:=xlValidateList, AlertStyle:=xlValidAltertStop, _ 
     Operator:=xlBetween, Formula1:="=" & myNamedRange 

    ... 
    ' skipping more property setting code ' 
    ... 
End With 
+0

可能重複的[如何保護Excel中的單元格,但允許這些被VBA腳本修改](http://stackoverflow.com/questions/125449/how-to-protect-cells-in-excel-but- allow-these-to-be-modified-by-vba-script) – 2014-10-31 09:39:52

回答

11

如果我理解正確的問題,你將成爲一個保護板。如果是這種情況,您可以使用以下VBA:

myWorksheet.Protect contents:=True, userinterfaceonly:=True 

這裏的關鍵部分是「userinterfaceonly:= true」。當使用此標誌集保護工作表時,VBA宏仍然可以進行更改。

將此代碼放入WorkBook_Activate事件中以自動保護工作簿並在激活時設置標誌。

編輯:感謝Lance Roberts對他的推薦使用Workbook_Activate而不是Workbook_Open

編輯:由於上述似乎沒有工作,你可能有解除包裹你的VBA代碼失敗的部分/保護命令。如果你這樣做,我也包住整個宏與錯誤處理程序,從而使錯誤之後的紙張沒有得不到保護:

Sub MyMacro 
    On Error Goto HandleError 

    ... 

    myWorksheet.unprotect 
    With ModifyCell.Validation 
     ... 
    End With 
    myWorksheet.protect contents:=True, userinterfaceonly:=True 

    ... 

Goto SkipErrorHandler 
HandleError: 
    myWorksheet.protect contents:=True, userinterfaceonly:=True 
    ... some code to present the error message to the user 
SkipErrorHandler: 
End Sub 

編輯:看一看this thread在PCreview。他們經歷了很多相同的步驟,並得出了相同的結論。至少你並不孤單!

+0

http://support.microsoft.com/kb/810788有關密碼的信息。 – Fionnuala 2009-01-15 15:48:16

2

我不確定這是否是一個通用的解決方案,但是當我最近遇到這個錯誤時,我必須在我做Validation.Add之前做一個MywkSheet.Activate。所以:

' set list values based on some conditions not defined for brevitity' 
If myCondition then 
    myNamedRange = "range1" 
Else 
    myNamedRange = "range2" 
End If 

''-------------------------------------------------- 
Sheets("mysheet").Activate 
''-------------------------------------------------- 

With modifyCell.Validation 
    .Delete 

    'Run time error occurs on the next line' 
    .Add Type:=xlValidateList, AlertStyle:=xlValidAltertStop, _ 
     Operator:=xlBetween, Formula1:="=" & myNamedRange 

    ... 
    ' skipping more property setting code ' 
    ... 
End With 

在我的情況,ScreenUpdating被關了,所以用戶不會看到牀單來回切換。 HTH。

相關問題