用VBA Dictionary做一个简单的用户系统
-
给大家建议这篇大佬Paul Kelly的文章关于VBA Dictionary的文章,讲的非常好https://excelmacromastery.com/vba-dictionary
首先,要使用Dictionary,我们需要勾选Microsoft Scripting Runtime这个library
-
在Excel里面输入两个按钮:
然后开始我们的操作,首先定义UserDataBase这个字典
Dim UserDataBase As New Dictionary
注册新的用户
Sub Register() Dim name As String, password As String ' 让用户输入用户名 name = Application.InputBox("Please input your user name") ‘如果用户名存在 If UserDataBase.Exists(name) Then MsgBox ("This user has registered!") Else ‘如果用户名不存在,那么加入用户名到字典 password = Application.InputBox("Please input your Password") UserDataBase(name) = password End If End Sub
-
登入
Sub login() Dim name As String, password As String '输入用户名 name = Application.InputBox("Please input your name") ‘如果用户名存在,且和密码可以匹配,那么问候这个人 If UserDataBase.Exists(name) Then password = Application.InputBox("Please input your Password") If password = UserDataBase(name) Then MsgBox ("Hello and welcome, " & name) Else '如果不存在,亲切地提示密码错误 MsgBox ("Your password is so fucking wrong") End If Else ‘如果用户名不存在,让他注册 MsgBox ("This user has not registered! Please register instead") End If End Sub
-
然后给我们的按钮赋予宏
点击注册
点击登入
因为这个密码对大小写敏感,提示密码错误