2012-10-19 44 views
0

我在需要保護的工作表上使用宏。這是宏:工作表重命名時宏不起作用

wksPartsDataEntry.Unprotect 
Sheet11.Unprotect 
Application.ScreenUpdating = False 

Dim historyWks As Worksheet 
Dim inputWks As Worksheet 

Dim nextRow As Long 
Dim oCol As Long 

Dim myCopy As Range 
Dim myTest As Range 

Dim lRsp As Long 

Set inputWks = wksPartsDataEntry 
Set historyWks = Sheet11 

'check for duplicate order ID in database 
If inputWks.Range("CheckID2") = True Then 
    lRsp = MsgBox("Clinic ID already in database. Update record?", vbQuestion + vbYesNo, "Duplicate ID") 
    If lRsp = vbYes Then 
    UpdateLogRecord 
    Else 
    MsgBox "Please change Clinic ID to a unique number." 
    End If 

Else 

    'cells to copy from Input sheet - some contain formulas 
    Set myCopy = inputWks.Range("OrderEntry2") 

    With historyWks 
     nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row 
    End With 

    With inputWks 
     Set myTest = myCopy.Offset(0, 2) 

     If Application.Count(myTest) > 0 Then 
      MsgBox "Please fill in all the cells!" 
      Exit Sub 
     End If 
    End With 

    With historyWks 
     With .Cells(nextRow, "A") 
      .Value = Now 
      .NumberFormat = "mm/dd/yyyy hh:mm:ss" 
     End With 
     .Cells(nextRow, "B").Value = Application.UserName 
     oCol = 3 
     myCopy.Copy 
     .Cells(nextRow, 3).PasteSpecial Paste:=xlPasteValues, Transpose:=True 
     Application.CutCopyMode = False 
    End With 

    'clear input cells that contain constants 
    With inputWks 
    On Error Resume Next 
     With myCopy.Cells.SpecialCells(xlCellTypeConstants) 
      .ClearContents 
      Application.GoTo .Cells(1) ', Scroll:=True 
     End With 
    On Error GoTo 0 
    End With 
End If 

Application.ScreenUpdating = True 
wksPartsDataEntry.Protect 
Sheet11.Protect 
End Sub 

宏工作正常。但是,我會將該文件分發給其他想要使用密碼保護表單的用戶。每個用戶都希望使用不同的密碼。 在代碼中添加密碼不是一種選擇,因爲該密碼是唯一的,我希望其他用戶在保護時能夠添加自己的密碼。 難道一個代碼存在,可以這樣做:

Sub Macro1() 
wksPartsDataEntry.Unprotect Password: (anything a user might choose as a password) 
Sheet11.Unprotect: (anything a user might choose as a password) 

因此,在基於用戶選擇自己的密碼,我的宏將取消保護並重新保護結束,而無需任何更改任何代碼的用戶。

希望我已經清楚了,謝謝你的回答!

回答

4

是有 - 你應該查找解除在Excel的幫助:

wksPartsDataEntry.Unprotect(password)是可能的,因此password是一個可選參數,這樣你就可以離開它。

爲了你需要做的,試試這個:

Public Sub MyUnprotect() 
    wksPartsDataEntry.Unprotect InputBox(_ 
       prompt:="Please type your password to unprotect:", _ 
       Title:="Unprotect") 

End Sub 

可以通過保存給定的密碼作爲變量擴大這種保護也即。

如果您想讓星星隱藏密碼輸入,您將不得不創建自己的UserForm,而不是InputBox。您可以在其中設置隱藏輸入的輸入字段。