【问题标题】:Mask text in Excel Input boxExcel 输入框中的屏蔽文本
【发布时间】:2020-08-25 09:14:25
【问题描述】:

我在 Excel 中制作了以下宏。如果您知道密码,它可以访问下拉列表的某些部分。唯一的问题是当你输入密码时文本是可见的。

如何使密码不是字符只是 * 或点?

    Option Explicit
Const human1 As String = "human1"
Const human2 As String = "human2"
Const human3 As String = "human3"

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim pwd As String
Dim Oops As Boolean

Application.EnableEvents = False

For Each cell In Target
If Not Intersect(cell, Range("L:L")) Is Nothing And cell <> "" Then
    pwd = Application.InputBox("Password for " & cell & ":", _
                "Enter Password", Type:=2)
    Select Case cell.Value
        Case "human1"
            If pwd <> human1 Then Oops = True
        Case "human2"
            If pwd <> human2 Then Oops = True
        Case "human3"
            If pwd <> human3 Then Oops = True

    End Select
    
    If Oops Then
        MsgBox "Bad password"
        cell = ""
    End If
End If
Next cell

Application.EnableEvents = True
End Sub

【问题讨论】:

    标签: excel vba inputbox


    【解决方案1】:

    您正在寻找以下功能。请阅读我对这篇文章的回答。 Masking Password in VBA Excel Input Box

    Option Explicit
    Private Declare PtrSafe Function CallNextHookEx Lib "user32" (ByVal hHook As LongPtr, _
        ByVal ncode As LongPtr, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    
    Private Declare PtrSafe Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As LongPtr
    
    Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
        (ByVal idHook As LongPtr, ByVal lpfn As LongPtr, ByVal hmod As LongPtr, ByVal dwThreadId As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function SendDlgItemMessage Lib "user32" Alias "SendDlgItemMessageA" _
    (ByVal hDlg As LongPtr, ByVal nIDDlgItem As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, _
    ByVal lpClassName As String, ByVal nMaxCount As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As LongPtr
    
    Private Const EM_SETPASSWORDCHAR = &HCC
    Private Const WH_CBT = 5
    Private Const HCBT_ACTIVATE = 5
    Private Const HC_ACTION = 0
    
    Private hHook As LongPtr
    
    
    Public Function NewProc(ByVal lngCode As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
        Dim RetVal
        Dim strClassName As String, lngBuffer As LongPtr
    
        If lngCode < HC_ACTION Then
            NewProc = CallNextHookEx(hHook, lngCode, wParam, lParam)
            Exit Function
        End If
    
        strClassName = String$(256, " ")
        lngBuffer = 255
    
        If lngCode = HCBT_ACTIVATE Then
            RetVal = GetClassName(wParam, strClassName, lngBuffer)
            If Left$(strClassName, RetVal) = "#32770" Then
                SendDlgItemMessage wParam, &H1324, EM_SETPASSWORDCHAR, Asc("*"), &H0
            End If
        End If
    
        CallNextHookEx hHook, lngCode, wParam, lParam
    End Function
    
    Public Function PasswordBox(Prompt, Title) As String
        Dim lngModHwnd As LongPtr, lngThreadID As LongPtr
    
        lngThreadID = GetCurrentThreadId
        lngModHwnd = GetModuleHandle(vbNullString)
    
        hHook = SetWindowsHookEx(WH_CBT, AddressOf NewProc, lngModHwnd, lngThreadID)
    
        PasswordBox = InputBox(Prompt, Title)
        UnhookWindowsHookEx hHook
    End Function
    

    在向模块声明此函数后,调用如下函数。

    Sub MaskedPassword()
        Debug.print PasswordBox("Enter your password.", "Paasword")
    End Sub
    

    终于在你的代码中采用函数...

    Option Explicit
    Const human1 As String = "human1"
    Const human2 As String = "human2"
    Const human3 As String = "human3"
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim pwd As String
    Dim Oops As Boolean
    
    Application.EnableEvents = False
    
    For Each cell In Target
    If Not Intersect(cell, Range("L:L")) Is Nothing And cell <> "" Then
    
        pwd = PasswordBox("Enter password for " & cell & ":", "Paasword")
    
        Select Case cell.Value
            Case "human1"
                If pwd <> human1 Then Oops = True
            Case "human2"
                If pwd <> human2 Then Oops = True
            Case "human3"
                If pwd <> human3 Then Oops = True
    
        End Select
        
        If Oops Then
            MsgBox "Bad password"
            cell = ""
        End If
    End If
    Next cell
    
    Application.EnableEvents = True
    End Sub
    

    【讨论】:

    • 感谢您的回答。我确实在模块中声明了该函数,但我无法让它工作。我认为我在代码中采用了错误的函数。我现在摸索了一下,但没有成功。在我的代码中,我应该在哪里采用下面的代码? Sub MaskedPassword() Debug.print PasswordBox("请输入您的密码。", "Paasword") End Sub
    • 我已经向你的代码展示了。请参阅答案的最后一部分。
    • 不需要用户debug.print语句。我已经展示了一个例子。
    • 这是在你的代码中使用函数pwd = PasswordBox("Enter password for " &amp; cell &amp; ":", "Paasword")
    • @Northie 如果您发现答案有用,请考虑接受答案。勾选答案。
    猜你喜欢
    • 2013-03-04
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多