excel - 在 VBA Excel 输入框中屏蔽密码

标签 excel vba passwords maskedtextbox

有人可以帮我屏蔽输入到使用以下代码生成的输入框中的密码。我将使用 Office 365 专业增强版。

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim sPassCheck As String
    Dim rng As Range
    Dim sTemp As String
    Dim sPassword As String

    sPassword = "12345"
    sTemp = "You must enter the password to delete data"

    ' Check if target is within Range N6:N100000
    If Intersect(Target, Range("N6:N100000")) Is Nothing Then

        If Target.Count > 1 Then
            Set rng = Target.Cells(1, 1)
        Else
            Set rng = Target
        End If


        If rng.Value = "" Then

            sPassCheck = InputBox(sTemp, "Delete check!")

            Application.EnableEvents = False

            If sPassCheck <> sPassword Then Application.Undo

        End If
    End If

    Application.EnableEvents = True
End Sub

最佳答案

上面的评论链接应该可以解决您的问题。这里就像相同的代码。首先将下面的代码复制并粘贴到模块中

enter image description here

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

然后从工作簿中的任何位置调用 PasswordBox() 函数。

Sub MaskedPassword()
    Range("A1") = PasswordBox("Enter your password.", "Paasword")
End Sub

关于excel - 在 VBA Excel 输入框中屏蔽密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63006614/

相关文章:

ruby-on-rails - Ruby on Rails,Devise gem 。密码为空时如何删除当前密码?

Javascript - 简单密码强度指示器

bash - 使用 `read` 读取密码时如何回显星号 (*)?

excel - 如何在 Excel 中组合多个嵌套 Substitute 函数?

excel - 将 Excel 图表复制到 Word 的 VBA 脚本在更高版本的 Word 中不起作用

Excel VBA : Reference a named range, 就像一个会引用图表或表格

excel - VBA 和 IE8 - 输入值和搜索

excel - 为什么 FIND 对结构化表不返回任何内容?

vba - Excel:对象文本框无法在 protected 与不 protected 工作表状态(带文本解锁)中执行回车...为什么?

excel - 填充系列之间的不规则间隙