如何使变量的值从模块到字符串保持不变?

时间:2022-04-16 14:02:11

I have been trying to create this password protection type thing in excel VBA for Applied ICT project. So I have created this module which protects all the sheet in the work books:

我一直在尝试在excel VBA中为Applied ICT项目创建这种密码保护类型的东西。所以我创建了这个模块来保护工作簿中的所有工作表:

Public Sub Auto_Open()

Dim ws As Worksheet
Dim psword As String

psword = "Unit 12"
For Each ws In Worksheets
    ws.Protect Password:=psword
Next ws

End Sub

Now the thing that i want to do is that whenever someone will open an userform an inputbox asking for the password will appear and if someone enters an invalid password a message box would appear. It will be something like this:

现在,我想要做的是,每当有人打开用户表单时,将出现一个输入框,询问密码,如果有人输入了无效密码,则会出现一个消息框。它将是这样的:

Private Sub UserForm_Activate()

If ActiveSheet.ProtectContents = True Then
x = InputBox("Please type in the password", "Password")
If x = psword Then
Call Module1.Sheets_unlock
Else
Msgbox("The password is invalid")
End
End If
End If

Now the problem is the of password changes to nothing in the UserForm_Activate() sub. I have tried the public psword as string, but it doesn't work. What can i do?

现在问题是UserForm_Activate()子中的密码更改为空。我已经尝试将公共psword作为字符串,但它不起作用。我能做什么?

1 个解决方案

#1


0  

Its where you Dim it.

它是你昏暗的地方。

Dim psword in a standard module above all other subs in the module like:

在标准模块中调暗psword,高于模块中所有其他子类,例如:

Public psword As String

#1


0  

Its where you Dim it.

它是你昏暗的地方。

Dim psword in a standard module above all other subs in the module like:

在标准模块中调暗psword,高于模块中所有其他子类,例如:

Public psword As String