密码按钮和用户表单 [英] Password button and userform

查看:40
本文介绍了密码按钮和用户表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Excel VBA中编写了以下用户窗体:

I have programmed the following userform in Excel VBA:

Private Sub CommandButton1_Click()
Password = TextBox1.Text
If Password = a Then
MsgBox "Password Correct.", vbInformation
Unload Me
Else
MsgBox "Password Incorrect. Please try again.", vbCritical
End If
End Sub

密码变量a ="test"

The password variable a = "test"

此密码用户表单本身可以完美运行.现在,我想将其连接到工作表中已经存在的按钮.此按钮后面的代码在VBA编辑器的"ThisWorkbook"部分中:

This password userform itself works perfectly. Now I want to connect it to a button that is already in the sheet. The code behind this button is in the section "ThisWorkbook" in the VBA editor:

Sub Button_Test()
UserForm1.Show
Tabelle2.Range("C3").Value = 1
End Sub

这个想法是按钮"Button_Test"受用户窗体可以输入的密码保护.正确输入密码后,将执行宏"Tabelle2.Range("C3").Value = 1.

The idea is that the button "Button_Test" is protected by a password that can be entered by the userform. Once the password is correctly entered the macro "Tabelle2.Range("C3").Value = 1" will be conducted.

但是,目前,我遇到的问题是,用户窗体使用密码正确"响应我,但没有启动宏"Tabelle2.Range("C3").Value = 1.

However, currently I have the issue that the userform responses me with "Password Correct" but does not start the macro "Tabelle2.Range("C3").Value = 1".

输入正确的密码后,如何告诉用户表单,应跳回到"Button_Test"宏并继续执行该过程?

How can I tell the userform once the password is entered correct it should jump back to the macro "Button_Test" and continue the procedure?

感谢您的帮助.

推荐答案

我将使用一个布尔值,当密码正确时,您将其设置为true.您可以做这样的事情:

I would work with a boolean value that you will set on true when the Password was correct. You Could make something like this:

模块或工作表中的代码

Sub Schaltfläche1_Klicken()
'Load funktion Check Password
If UserForm1.checkPassword() = True Then
    'Run this Code when PWD was Correct
    Tabelle1.Range("C3").Value = 1
End If
End Sub

,然后在用户窗体中输入如下代码:

Private passwordStatus As Boolean

Private Sub CommandButton1_Click()
    Dim a As String
    Dim Password As String

    a = "123"
    Password = TextBox1.Text
    'Set Pawwordstatus at False before Testing
    passwordStatus = False
    If Password = a Then
        MsgBox "Password Correct.", vbInformation
        passwordStatus = True
        Unload Me
    Else
        MsgBox "Password Incorrect. Please try again.", vbCritical
    End If
End Sub

Function checkPassword() As Boolean
  UserForm1.Show
  'Shows the User Form. And after Closing the Form
  'The PasswordStatus Value will be returned and you can check if
  'it is true
  checkPassword = passwordStatus
End Function

这篇关于密码按钮和用户表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆