如果链接的工作表受保护,我的macros将不会运行

这个macros的目的是检查前面的用户名和密码是否与订阅特定人员的密码相匹配,然后将用户名和密码无限期地放在“员工薪水”表上。

我所遇到的麻烦是阻止某人更改已经植入的用户名和密码。我locking了单元格,因为表单已被保护,这似乎阻止了macros的工作,因为它表示如果它们被locking,它将无法更改单元格。

有没有其他方法可以用来防止冲突?

Sub PASSWORD_CHECK() Application.Worksheets("Front sheet").Range("B24").Formula = ("=VLOOKUP(D4,Lock!R:T,1,FALSE)") ' looks up school name Application.Worksheets("Front sheet").Range("B25").Formula = ("=EXACT(B24, D4)") ' validation of school name Application.Worksheets("Front sheet").Range("B26").Formula = ("=VLOOKUP(D4,Lock!R3:T106,3,FALSE)") ' looks up password Application.Worksheets("Front sheet").Range("B27").Formula = ("=EXACT(B26,D6)") ' validates password Application.Worksheets("Front sheet").Range("B29").Formula = ("=IF(AND(B25,B27),1,0)") ' checks to validate both school and pasword Application.Worksheets("Front sheet").Range("B32").Formula = ("=IF(B27=B25,1,0)") ' Checks to see if password belongs to school Dim A As String Dim B As String Dim C As Boolean Dim D As String Dim E As String Dim F As Boolean A = Worksheets("Staff Salaries").Range("P3") B = Worksheets("Staff Salaries").Range("N2") C = Worksheets("Front sheet").Range("B29") D = Worksheets("Front sheet").Range("D4") E = Worksheets("Front sheet").Range("D6") F = Worksheets("Front sheet").Range("B32") If C = "TRUE" And F = "TRUE" Then ' All OK Worksheets("Staff Salaries").Range("N2") = D Worksheets("Staff Salaries").Range("P3") = E Else MsgBox ("PASSWORD IS INCONSISTANT WITH USER") End If End Sub 

尝试这个:

  Sub PASSWORD_CHECK() Dim wb As Workbook Dim ws As Worksheet Set wb = ThisWorkbook Set ws = wb.Sheets("Sheet1") ' Change the name of the sheet which is locked ws.Unprotect Password:="YourPassWord" ' Type your password Application.Worksheets("Front sheet").Range("B24").Formula = ("=VLOOKUP(D4,Lock!R:T,1,FALSE)") ' looks up school name Application.Worksheets("Front sheet").Range("B25").Formula = ("=EXACT(B24, D4)") ' validation of school name Application.Worksheets("Front sheet").Range("B26").Formula = ("=VLOOKUP(D4,Lock!R3:T106,3,FALSE)") ' looks up password Application.Worksheets("Front sheet").Range("B27").Formula = ("=EXACT(B26,D6)") ' validates password Application.Worksheets("Front sheet").Range("B29").Formula = ("=IF(AND(B25,B27),1,0)") ' checks to validate both school and pasword Application.Worksheets("Front sheet").Range("B32").Formula = ("=IF(B27=B25,1,0)") ' Checks to see if password belongs to school Dim A As String Dim B As String Dim C As Boolean Dim D As String Dim E As String Dim F As Boolean A = Worksheets("Staff Salaries").Range("P3") B = Worksheets("Staff Salaries").Range("N2") C = Worksheets("Front sheet").Range("B29") D = Worksheets("Front sheet").Range("D4") E = Worksheets("Front sheet").Range("D6") F = Worksheets("Front sheet").Range("B32") If C = "TRUE" And F = "TRUE" Then ' All OK Worksheets("Staff Salaries").Range("N2") = D Worksheets("Staff Salaries").Range("P3") = E Else MsgBox ("PASSWORD IS INCONSISTANT WITH USER") End If ws.Protect Password:="YourPassWord" End Sub