授予特定用户帐户访问权限时出错

时间:2019-04-27 16:26:41

标签: excel vba

不明白为什么以下结果会导致编译错误,请帮忙。

在VBA中遇到的问题,试图创建具有适当访问权限的用户帐户访问权限,但与标记相差不远

Sub CheckUser()
Dim UserRow, SheetCol As Long
Dim SheetNm As String
With Sheet1
.Calculate

If .Range("B8").Value = Empty Then 'Incorrect Username
MsgBox "Please enter a correct Username"
Exit Sub
End If

If .Range("B7").Value <> True Then 'Incorrect password
MsgBox "Please enter correct Password"
Exit Sub
End If

LoginForm.Hide
.Range("B5,B6").ClearContents
UserRow = .Range("B8").Value 'User row

For SheetCol = 7 To 19
SheetNm = .Cells(5, SheetCol).Value 'Sheet Name
If .Cells(UserRow, SheetCol).Value = "Ð" Then
Sheets(SheetNm).Unprotect "123"
Sheets(SheetNm).Visible = xlSheetVisible
End If

If .Cells(UserRow, SheetCol).Value = "Ï" Then
Sheets(SheetNm).Protect "123"
Sheets(SheetNm).Visible = xlSheetVisible

If .Cells(UserRow, SheetCol).Value = "x" Then Sheets(SheetNm).Visible = xlSheetVeryHidden
Next SheetCol
End With
End Sub

Sub CloseworkBook()
Sheet15 Activate
Dim WkSht As Worksheet
For Each WkSht In ThisWorkbook.Worksheets
If WkSht <> "Start" Then WkShy.Visible = xlSheetVeryHidden
Next WkSht
ThisWorkbook.Save
End Sub

以为我过得很好...猜不到

期待宏正常运行,但失败

1 个答案:

答案 0 :(得分:0)

正如评论所提到的,您的代码中有一些错别字和错误。我已经快速浏览了所有可以找到的内容。另外,我建议您使用rubberduck com加载项。它具有许多强大的功能,可帮助您在代码中improve and spot errors

Option Explicit 'Always add this to all your modules (in VBE--> tools--> Require Variable declaration)

Sub CheckUser()
Dim UserRow, SheetCol As Long
Dim SheetNm As String
Dim Sheet1 As Worksheet 'declare worksheet
Set Sheet1 =Sheets("Sheet1") 'set worksheet

With Sheet1
.Calculate

If .Range("B8").Value = Empty Then 'Incorrect Username
MsgBox "Please enter a correct Username"
Exit Sub
End If

If .Range("B7").Value <> True Then 'Incorrect password
MsgBox "Please enter correct Password"
Exit Sub
End If

LoginForm.Hide
.Range("B5,B6").ClearContents
UserRow = .Range("B8").Value 'User row

For SheetCol = 7 To 19
SheetNm = .Cells(5, SheetCol).Value 'Sheet Name
If .Cells(UserRow, SheetCol).Value = "Ð" Then
Sheets(SheetNm).Unprotect "123"
Sheets(SheetNm).Visible = xlSheetVisible
End If

If .Cells(UserRow, SheetCol).Value = "Ï" Then
Sheets(SheetNm).Protect "123"
Sheets(SheetNm).Visible = xlSheetVisible
End If ' added missing End if statement

If .Cells(UserRow, SheetCol).Value = "x" Then Sheets(SheetNm).Visible = xlSheetVeryHidden
Next SheetCol
End With
End Sub

Sub CloseworkBook()
Sheet15.Activate 'Sheet 15.Activate Not Sheet15 Activate
Dim WkSht As Worksheet
For Each WkSht In ThisWorkbook.Worksheets
If WkSht <> "Start" Then WkSht.Visible = xlSheetVeryHidden 'edited WkShy to the correct WkSht
Next WkSht
ThisWorkbook.Save
End Sub