Excel 用vba中的另一个宏更改一个宏中的变量
这里是第二个帖子。我所要做的就是更改密码以保护和取消保护我的工作簿,如我在这里的代码中定义的那样Excel 用vba中的另一个宏更改一个宏中的变量,excel,vba,Excel,Vba,这里是第二个帖子。我所要做的就是更改密码以保护和取消保护我的工作簿,如我在这里的代码中定义的那样 Dim myPassword As String myPassword = "yogurt" 'defines the password For Each sh In ActiveWorkbook.Worksheets 'unprotects the sheet for editing sh.Unprotect Password:=myPasswo
Dim myPassword As String
myPassword = "yogurt" 'defines the password
For Each sh In ActiveWorkbook.Worksheets 'unprotects the sheet for editing
sh.Unprotect Password:=myPassword
Next sh
…通过使用另一个名为“更改密码”的宏,用户将输入当前密码,然后能够输入新密码
Option Explicit
Public badpassword As Boolean
Sub changepassword()
Dim sh As Worksheet
Dim pw1 As String
Dim newpw As String
Dim newpw2 As String
badpassword = True
'enter the current password, twice
pw1 = enterpassword("Please enter the password to UNPROTECT the sheets")
'prompt for a new password
newpw = enterpassword("Please enter the new password")
newpw2 = enterpassword("Please re-enter the new password")
If newpw <> newpw2 Then
'## inform the user that the passwords don't match
MsgBox "The passwords are not the same", vbCritical
Else:
'## Attempt to change the password on each sheet
For Each sh In ActiveWorkbook.Worksheets
On Error GoTo badpassword '## provide a means of escaping error if password is incorrect
protectsheet sh, pw1, newpw
On Error GoTo 0
If badpassword Then
MsgBox "The password you entered is incorrect for sheet:" & sh.Name _
, vbCritical
'## allow the macro to continue on other worksheets:
badpassword = False
End If
Next
End If
Exit Sub
badpassword:
'## Trap errors if the supplied password is invalid
badpassword = True
Resume Next
End Sub
Function enterpassword(Optional msg As String = "Please enter the password")
Dim pw$
pw = InputBox(msg, "Password?")
enterpassword = pw
End Function
Sub protectsheet(sh As Worksheet, pw As String, newpw As String)
sh.Unprotect pw
sh.protect newpw
badpassword = False 'indicates a success
End Sub
我只希望“更改密码”宏在用户键入两次新密码以确保准确性时起作用
有什么快速的建议吗
非常感谢
Sub change_password()
Dim OldPassword, MyPassword, NewPassword As String
Dim pass1, pass2
MyPassword = monkey
OldPassword = InputBox("Please enter the old password.")
If OldPassword = MyPassword Then
pass1 = InputBox("Enter the new password.")
pass2 = InputBox("Enter the new password again to ensure accuracy.")
If pass1 = pass2 Then
MyPassword = pass1
Else
MsgBox "The new password you entered was not entered correctly both times."
End If
End If
MsgBox ("Your new password is" & MyPassword)
End Sub
虽然简单地调用对话框来设置工作簿保护可能更容易(即,如果不同的工作表需要不同的密码,此方法将有错误,我尝试捕获此类错误)并使用内置对话框,但这将基本满足您的要求 一如既往,记住你的密码。我没有提供检索丢失密码的方法
Option Explicit
Public badpassword As Boolean
Sub changepassword()
Dim sh As Worksheet
Dim pw1 As String
Dim newpw As String
Dim newpw2 As String
badpassword = True
'enter the current password, twice
pw1 = enterpassword("Please enter the password to UNPROTECT the sheets")
'prompt for a new password
newpw = enterpassword("Please enter the new password")
newpw2 = enterpassword("Please re-enter the new password")
If newpw <> newpw2 Then
'## inform the user that the passwords don't match
MsgBox "The passwords are not the same", vbCritical
Else:
'## Attempt to change the password on each sheet
For Each sh In ActiveWorkbook.Worksheets
On Error GoTo badpassword '## provide a means of escaping error if password is incorrect
protectsheet sh, pw1, newpw
On Error GoTo 0
If badpassword Then
MsgBox "The password you entered is incorrect for sheet:" & sh.Name _
, vbCritical
'## allow the macro to continue on other worksheets:
badpassword = False
End If
Next
End If
Exit Sub
badpassword:
'## Trap errors if the supplied password is invalid
badpassword = True
Resume Next
End Sub
Function enterpassword(Optional msg As String = "Please enter the password")
Dim pw$
pw = InputBox(msg, "Password?")
enterpassword = pw
End Function
Sub protectsheet(sh As Worksheet, pw As String, newpw As String)
sh.Unprotect pw
sh.protect newpw
badpassword = False 'indicates a success
End Sub
选项显式
公共密码为布尔值
子密码()
将sh设置为工作表
作为字符串的Dim pw1
作为字符串的Dim newpw
Dim newpw2作为字符串
badpassword=True
'输入当前密码,两次
pw1=输入密码(“请输入密码以解除对工作表的保护”)
'提示输入新密码
newpw=输入密码(“请输入新密码”)
newpw2=输入密码(“请重新输入新密码”)
如果newpw newpw2那么
“##通知用户密码不匹配
MsgBox“密码不相同”,vbCritical
其他:
“##尝试更改每张工作表上的密码
对于ActiveWorkbook.工作表中的每个sh
错误转到badpassword'##时,如果密码不正确,请提供一种逃避错误的方法
保护纸sh,pw1,新PW
错误转到0
如果密码不正确,那么
MsgBox“您为工作表输入的密码不正确:&sh.Name_
,vbCritical
“##允许宏在其他工作表上继续:
badpassword=False
如果结束
下一个
如果结束
出口接头
密码:
“##如果提供的密码无效,则捕获错误
badpassword=True
下一步继续
端接头
函数enterpassword(可选消息为String=“请输入密码”)
暗pw$
pw=输入框(消息“密码?”)
输入密码=pw
端函数
子工作表(sh作为工作表,pw作为字符串,newpw作为字符串)
sh.解除保护pw
sh.protect newpw
badpassword=False'表示成功
端接头
密码必须存储在某个地方。我在下面的代码中使用了一个范围,并将其命名为passwordrange(“password”)
我想我当时的问题不清楚。我想通过在输入框中键入来更改第一个子项中的密码。在这里,我将写一些代码来帮助澄清。Sub change_password()Dim OldPassword,MyPassword,NewPassword作为字符串Dim pass1,pass2 MyPassword=monkey OldPassword=InputBox(“请输入旧密码”)。如果OldPassword=MyPassword,那么pass1=InputBox(“输入新密码”)。pass2=InputBox(“再次输入新密码以确保准确性。”)如果pass1=pass2,则MyPassword=pass1 Else MsgBox“您输入的新密码两次都没有正确输入。”如果End If MsgBox(“您的新密码是”&MyPassword),则结束结束子编辑您的问题-注释中的代码很难理解。抱歉。我正在试图找出如何正确发布。当我发布宏中的代码时,结果是这样的。我如何在该网站的行之间放置空格?您不能在注释中的行之间放置空格,您需要编辑您的帖子正文。谢谢!我是初学者o很多代码对我来说没有任何意义。我不怀疑它是否有效,但我会更轻松地使用Santosh提供的代码。我会投票赞成,但我没有足够的声誉…这对你来说没有意义,只是必须有效:)我相信Santosh的方法有一个安全问题,即密码可能对任何人都可见。@DavidZemens of Course有一些方法可以隐藏一个有密码的单元格,你会更好地意识到这一点。这看起来很好!我想我可以遵循这一点。我会在这个周末的某个时候尝试将其全部输入到我的工作簿中!现在我会将其标记为已解决。如果你将密码存储在一个指定的范围内,它是有潜力的任何人都看不见,哪一种破坏了保护工作表的目的。还是我遗漏了什么?他们只需要受到保护,以防意外的编辑会弄乱工作表。我正在为一家连锁冰淇淋店的经理编写此程序。反正他们有密码。所以我想我几乎理解了此代码以及如何使用它差不多了。有两件事,1。更改密码blnPassword这一行是做什么的?2。我应该在“您的cod在这里”中输入哪一个代码?谢谢。还有一个问题……您的代码的第一行中的“(ByRef blnIsChanged)”是什么意思?我觉得这很重要。